Mart R
Mart R

Reputation: 126

Cannot find element inside div with xpath or css selector

I am trying to get "January 27" as output with this code:

task_offer_deadline = driver.find_element_by_xpath('//*[@id="table"]/div[2]/div/a/div[1]/span[2]/div/em').text

the xpath provided by Google Chrome Console. This was the html source:

<div class="content table-contents">
    <a class="one is-incomplete" href="/authors/tasks/5e2ad33c2b3dc80013d3dba6">
        <div class="left part">
            <span class="ref">
                <em>C33-8823</em>
                <strong class="date"></strong>
            </span>
            <span class="deadline-or-countdown">
                <div class="deadline-at">
                    <em class="green">January 27</em>
                </div>
            </span>

but it cannot be found. I even tried with other types of search, id, class name, all to no avail. Any clues why? thanks a lot!

Upvotes: 3

Views: 1251

Answers (3)

Mart R
Mart R

Reputation: 126

It happened the page had some display filters that change the url and I was searching on the wrong url.

adding the filter parameters to the address ?statuses%5Bin_progress%5D=1. made it work. Thanks for having a look everyone!

Upvotes: 1

Muzzamil
Muzzamil

Reputation: 2881

Try with below css using webdriver wait with visibility of element

xpath = "//em[.= 'C33-8823']/../..//em[2]"
element = WebDriverWait(driver, 20)
    .until(EC.visibility_of_element_located((By.XPATH, xpath)))
element.text

you need below import

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

Upvotes: 2

Evgeniy Chiruk
Evgeniy Chiruk

Reputation: 358

Try this xpath:

xpath = //span[@class='deadline-or-countdown']/div[@class='deadline-at']/em

If this also doesn’t help, then make sure your element is not inside the <iframe>.

If the element is in the iframe, then you need to switch to it before looking for the element:

iframe = driver.find_element_by_xpath("//iframe[@name='Dialogue Window']")
driver.switch_to.frame(iframe)

Upvotes: 2

Related Questions