python - 如何通过 Selenium 和 python 点击​​ find_element_by_* 的链接

标签 python selenium xpath css-selectors webdriver

我需要点击收件箱中的确认邮件。但无法点击该链接。

这是我的 html 和尝试过的代码

<td bgcolor="#ffffff" style="padding:20px;">
<div style="color:rgb(0,0,0);font-size:16px;">
<strong>You are almost done!</strong><br><br>
    To complete Sunday Lankadeepa E-Paper registration, please click the link below:
</div>
<br><br>
<div style="width:616px;">
<a href="http://Sundaylankadeepa.newspaperdirect.com/epaper/confirmmail.aspx?code=ZUZ3S2K17GR&amp;rt=trial" rel="nofollow">

http://Sundaylankadeepa.newspaperdirect.com/epaper/confirmmail.aspx?code=ZUZ3K17GR&amp;rt=trial</a>
</div>
<br><br>
If clicking the link doesn't work, please select and copy the entire link. Then open a browser, paste the link in the address bar, and press Enter or Return on your keyboard.

尝试过代码...

text = "newspaperdirect"
element = driver.find_element_by_xpath('//a[contains(text(),"%s")]' % text)
element.click()

错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: 
{"method":"xpath","selector":"//a[contains(text(),"newspaperdirect")]"}
 (Session info: chrome=71.0.3578.98)
  (Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),
platform=Windows NT 6.2.9200 x86_64)

最佳答案

添加一些等待并使用find_element_by_partial_link_text .

import time
from selenium import webdriver
browser = webdriver.Chrome(executable_path='/home/bitto/chromedriver')
url='file:///test.html' # url or .html file path here
text = "newspaperdirect"
browser.implicitly_wait(20) # seconds
browser.get(url)
browser.find_element_by_partial_link_text(text).click()

关于python - 如何通过 Selenium 和 python 点击​​ find_element_by_* 的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53903371/

相关文章:

internet-explorer - 使用IE Developer工具查找XPath元素

python - 403 权限被拒绝从 PubSub 访问 AutoML

Java xpath 将整个元素作为字符串返回

python - python 中的 string.replace 方法

python - 在 Selenium 上切换窗口

python - 网页抓取 : how to extract this kind of div tag?

java - 当无法通过系统属性或路径指定 geckodriver 位置时,是否有非远程方法来指定 geckodriver 位置?

java - 获取页面中多次出现的div的xpath

python - 如何使用python保存内存、pid和进程

python - os.environ 不返回新的和导出的环境变量