Selenium Webdriver:单击在Python中不起作用

时间:2018-09-08 20:13:49

标签: python selenium

我想首先单击带有文本A218012216的表格链接。
表/链接代码似乎隐藏在JS中。
我尝试了几种方法,但没有成功。

到目前为止,这是我的代码:

import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
url0 ="https://ccrecordse.tarrantcounty.com/AssumedNames/SearchEntry.aspx"
driver = webdriver.Chrome(executable_path="D:\Python\chromedriver.exe")
driver.get(url0)
time.sleep(3)
#fill the form # select by visible text
selectStart = driver.find_element_by_id('x:11265151.0:mkr:3')
selectStart.send_keys('09/05/2019')
selectEnd = driver.find_element_by_id('x:1246303050.0:mkr:3')
selectEnd.send_keys('09/05/2019')
#submit the form
driver.find_element_by_id("cphNoMargin_SearchButtons2_btnSearch__5").click()
time.sleep(3)
driver.find_element_by_link_text('A218012216').click()

如何获取该信息?

1 个答案:

答案 0 :(得分:0)

问题是您试图在非find_element_by_link_text标签的元素上使用<a> link_text仅用于<a>标签...

在我的解决方案中,您将看到我正在使用WebDriverWait,这是硒中的最佳做法...

此外,我将XPath定位器与text()=the_text一起使用,请注意,the_text将需要随着日期的更改而更改(您正在搜索将来的日期09/05/2019,因此它将显示当前日期)。文本的日期将更改...)

解决方案:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

url0 ="https://ccrecordse.tarrantcounty.com/AssumedNames/SearchEntry.aspx"

driver = webdriver.Chrome(executable_path=r"D:\Python\chromedriver.exe")

driver.get(url0)
time.sleep(3)
#fill the form # select by visible text
selectStart = driver.find_element_by_id('x:11265151.0:mkr:3')
selectStart.send_keys('02/19/2019')
selectEnd = driver.find_element_by_id('x:1246303050.0:mkr:3')
selectEnd.send_keys('02/19/2019')
#submit the form
driver.find_element_by_id("cphNoMargin_SearchButtons2_btnSearch__5").click()
the_text = "A219002410"
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[text()='"+the_text +"']"))).click()

希望这对您有帮助!