使用 xpath 或 css 选择器无法在 div 中找到元素

Cannot find element inside div with xpath or css selector

我正在尝试使用以下代码将 "January 27" 作为输出:

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

Google Chrome 控制台提供的 xpath。这是 html 来源:

<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>

但是找不到。我什至尝试了其他类型的搜索,id,class name,都无济于事。 任何线索为什么?非常感谢!

试试这个 xpath:

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

如果这也没有帮助,请确保您的元素不在 <iframe>.

如果元素在iframe中,那么在查找元素之前需要切换到它:

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

尝试使用下面的 css 使用 webdriver 等待 visibility 元素

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

你需要下面的导入

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

碰巧该页面有一些更改 url 的显示过滤器,而我搜索错误 url。

将过滤器参数添加到地址 ?statuses%5Bin_progress%5D=1. 使其工作。感谢大家的观看!