-2

我可以使用 Python 和 Selenium 定位网页的元素吗?有没有办法可以找到特定文本是否存在于同一个元素中。

下面是我的代码:

import datetime, time
from selenium import webdriver
from pathlib import Path

options = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path=r'C:\chromedriver_win32\chromedriver.exe', chrome_options=options)
driver.implicitly_wait(30)
today = time.strftime("%d/%m/%Y")
print(today)
driver.get("http://mylink.com")
uname = driver.find_element_by_name("UserName")
pwd = driver.find_element_by_name("Password")
uname.send_keys("@.com")
pwd.send_keys("pwd")
driver.find_element_by_xpath('//*[@id="submitButton"]').click()

time.sleep(30)

element=driver.find_element_by_xpath('//*[@id="block-system-main"]/div/div/div/div[3]/div/div/div[2]/div/div[2]/table/tbody/tr[1]/td[6]/span/font/font')

Belos 是我得到的错误。

 selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="block-system-main"]/div/div/div/div[3]/div/div/div[2]/div/div[2]/table/tbody/tr[1]/td[6]/span/font/font"}
4

1 回答 1

0

要等待元素中存在特定文本,您可以使用以下解决方案:

WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element_by_xpath("//nodeTag[contains(.,'text_to_be_present')]")))
于 2018-06-12T14:55:58.120 回答