如何通过Selenium和python单击带有find_element_by_ *的链接 - python

我需要在收件箱中单击确认邮件。但无法单击链接。

这是我的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&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:在不更改段落顺序的情况下在文件的每个段落中反向单词? - python

我想通过反转text_in.txt文件中的单词来生成text_out.txt文件,如下所示:text_in.txt具有两段,如下所示:Hello world, I am Here. I am eighteen years old. text_out.txt应该是这样的:Here. am I world, Hello old. years eighteen a…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

如何在python中将从PDF提取的文本格式化为json - python

我已经使用pyPDF2提取了一些文本格式的发票PDF。我想将此文本文件转换为仅包含重要关键字和令牌的json文件。输出应该是这样的:#PurchaseOrder {"doctype":"PO", "orderingcompany":"Demo Company", "su…

Python:将两列组合在一起,找到第三列的总和 - python

python真的很新,需要我完成的问题需要一些帮助。我需要根据用户对月份(MM)和年份(YYYY)的输入来找到每个时间段(月/年)的平均收入。我的输入如下:year_value = int(input("Year (YYYY): ")) month_value = int(input("Month (MM): ")) …