Python Selenium - 如何检查 HTML 上是否有包含此内容的项目?

Python Selenium - How do I check if we have an item with this on HTML?

我需要检查我们是否在页面上使用某些东西作为触发器:

data-id="false_5511971198499@c.us_DBC6E6D07C76B179C26A40D689B2AEB2"

但是,我只需要检查 "data-id="false_",因为其他的都可以。

我尝试创建:

element = driver.find_element_by_xpath("[data-id='False']")

但是没用。而且我不知道如何继续,如果有这个元素我们需要 print(ok)

你能帮帮我吗?

谢谢!

试试这个

//*[starts-with(@data_id,'false_')]

它是一个动态元素,因此您必须引入 for the visibility_of_element_located() and you can use either of the following :

  • 使用XPATH:

    WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[starts-with(@data-id, 'false_')]")))
    
  • 使用CSS_SELECTOR:

    WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[data-id^='false_']")))
    
  • 注意:您必须添加以下导入:

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