Randumb Purson
Randumb Purson

Reputation: 66

Can't find element selenium python with xpath

I'm a relative newcomer to selenium so this might be something incredibly simple but I can't seem to access an element even though it appears on the page. I don't think it can be that it hasn't loaded yet because I can reference other elements. The line of code I am trying to use and the html is below.

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>

Upvotes: 0

Views: 753

Answers (1)

Moshe Slavin
Moshe Slavin

Reputation: 5214

You can use WebDriverWait with expected_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)

Upvotes: 1

Related Questions