无法在python中使用Selenium WebDriver单击链接 [英] Unable to click link using selenium webdriver in python

查看:149
本文介绍了无法在python中使用Selenium WebDriver单击链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试点击下面显示的链接:

I am trying to click on the link shown below:

<a class="user" href="/Kevin-Rose" action_mousedown="UserLinkClickthrough" id="__w2_L73qRYl_link" 
target="_blank">
         <span class="matched_term">Kevin Rose</span>
</a>

我尝试使用以下代码:

 user = driver.find_element_by_xpath("//a[@class='user'][1]")
 user.click()

我遇到以下错误:

ElementNotVisibleException:消息:u'Element当前不可见,因此可能无法与之交互';堆栈跟踪:

ElementNotVisibleException: Message: u'Element is not currently visible and so may not be interacted with' ; Stacktrace:

我该如何解决这个问题?

How can I solve this problem?

推荐答案

问题是您正在查找元素,而该元素尚不存在. Wait .

The problem is that you are finding the element while it is not yet there. Wait for it.

这是完整的代码,从登录,搜索到跟随第一个Kevin Rose链接:

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()

这篇关于无法在python中使用Selenium WebDriver单击链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆