I am trying to click on the link shown below:
I tried using following code:
user = driver.find_element_by_xpath("//a[@class='user'][1]")
user.click()
I got following error:
ElementNotVisibleException: Message: u'Element is not currently visible and so may not be interacted with' ; Stacktrace:
How can I solve this problem?
解决方案
The problem is that you are finding the element while it is not yet there. Wait for it.
Here's the complete code, from logging in, search to following the first Kevin Rose link:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
URL = "http://www.quora.com"
EMAIL = 'your email here'
PASSWORD = 'your password here'
driver = webdriver.Chrome() # can be webdriver.Firefox() in your case
driver.get(URL)
wait = WebDriverWait(driver, 10)
# login
form = driver.find_element_by_class_name('regular_login')
username = form.find_element_by_name('email')
username.send_keys(EMAIL)
password = form.find_element_by_name('password')
password.send_keys(PASSWORD)
login_button = form.find_element_by_xpath('//input[@type="submit" and @value="Login"]')
login_button.click()
# search
search = wait.until(EC.presence_of_element_located((By.XPATH, "//form[@name='search_form']//input[@name='search_input']")))
search.send_keys('Kevin Rose')
search.send_keys(Keys.ENTER)
# follow the link
link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Kevin Rose")))
link.click()