如何在Selenium中访问第二个div节点(Python)

 收藏

以下HTML代码将继续进行:

<div class="lower-text">
<div data-text="winScreen.yournumberis">Your number is:</div>
<div>Randomly generated number</div>`

我要访问的是“ lower-text”类中的第二个div节点(随机生成的数字)

 try:
    element = WebDriverWait(browser, 10).until(
        EC.visibility_of_element_located((By.CLASS_NAME, 'lower-text'))
    )
finally:
    found = browser.find_element_by_xpath("//a[@class='lower-text'][2]").text
    print(found)

这是我尝试的没有成功的方法。 我不确定如何访问第二个div节点。任何帮助,将不胜感激。

回复
  • 请尝试以下解决方案::

    element = WebDriverWait(driver, delay).until(
            EC.presence_of_element_located((By.XPATH, "//*[@data-text='winScreen.yournumberis']/following-sibling::div")))
        print element.text
    

    另外您需要添加以下导入

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