且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从 <p> 获取文本使用 XPath Selenium 和 Python 标记

更新时间:2023-02-19 16:59:45

打印文本 Content-type: text/plain;charset=us-ascii 你必须诱导 WebDriverWait对于 visibility_of_element_located(),您可以使用以下任一定位器策略:

To print the text Content-type: text/plain; charset=us-ascii you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • 使用 XPATHtext 属性:

driver.get("https://www.w3.org/Protocols/rfc1341/7_1_Text.html")
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3[contains(., 'The charset parameter')]//following-sibling::p[2]"))).text)

  • 使用 XPATHget_attribute():

    driver.get("https://www.w3.org/Protocols/rfc1341/7_1_Text.html")
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3[contains(., 'The charset parameter')]//following-sibling::p[2]"))).get_attribute("innerHTML"))
    

  • 控制台输出:

  • Console Output:

    Content-type: text/plain; charset=us-ascii
    

  • 注意:您必须添加以下导入:

  • Note : You have to add the following imports :

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