python selenium 点击链接空白,无法在python中使用Selenium WebDriver单击链接

I am trying to click on the link shown below:

target="_blank">

Kevin Rose

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?

2fe3ca438e5d3158276831572c67196b.png

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值