用xpath找不到元素Selenium python

时间:2019-01-10 16:24:13

标签: python selenium-webdriver

我是硒的新手,所以这可能非常简单,但是即使它出现在页面上,我似乎也无法访问它。我认为可能还没有加载,因为我可以引用其他元素。我尝试使用的代码行和html在下面。

max_questions = driver.find_element_by_xpath(xpath="//span[contains(@class, 'total-questions')]")

<div data-v-404a90e7="" data-v-084771db="" class="header animated fadeInDown anim-300-duration">
    <div data-v-404a90e7="" class="left-section half-width">
        <div data-v-404a90e7="" flow="right" class="menu-icon animated fadeIn anim-300-duration">
            <div data-v-404a90e7="" class="menu-icon-image"></div>
        </div>
        <div data-v-404a90e7="" class="question-number-wrapper text-unselectable animated fadeIn anim-300-duration">
            <span data-v-404a90e7="" class="current-question">1</span>
            <span data-v-404a90e7="" class="total-questions">/10</span>
        </div>
    </div>
    <div data-v-404a90e7="" class="right-section half-width">
        <div data-v-404a90e7="" class="room-code animated fadeIn anim-300-duration">712851</div>
        <div data-v-404a90e7="" flow="left" class="exit-game-btn-wrapper animated fadeIn anim-300-duration">
            <div data-v-404a90e7="" class="exit-game-icon"></div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

您可以将WebDriverWaitexpected_conditions一起使用:

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

driver = webdriver.Chrome('d:\\chromedriver\\chromedriver.exe')
driver.get(url)
wait = WebDriverWait(driver, 10)
max_questions = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(@class, 'total-questions')]")))
print(max_questions.text)