代码之家  ›  专栏  ›  技术社区  ›  Shashi Shankar Singh

如何使用python selenium检查元素中是否存在文本?

  •  -2
  • Shashi Shankar Singh  · 技术社区  · 7 年前

    我能够使用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')
    

    贝洛斯是我得到的错误。

     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"}
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   undetected Selenium    7 年前

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

    WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element_by_xpath("//nodeTag[contains(.,'text_to_be_present')]")))