简体   繁体   English

如何在 Selenium - Python 中通过它们的 innerText 定位元素

[英]How to locate elements by their innerText in Selenium - Python

I'm trying to locate and click on buttons by using their "innerText' (in my case: 'Sprint 1', 'Sprint 2', and 'Sprint 3')我正在尝试使用它们的“innerText”(在我的例子中:“Sprint 1”、“Sprint 2”和“Sprint 3”)找到并单击按钮

Here is a part of my html:这是我的 html 的一部分:

<div class="show dropdown">
    <div class="dropdown-menu show">
        <a href="#" class="dropdown-item active" role="button">
            <div class="period-title">
                <span><span class="badge badge-success">active</span><span class="abc-name">Sprint 1</span></span>
            </div>
        </a>
        <a href="#" class="dropdown-item" role="button">
            <div class="period-title">
                <span><span class="badge badge-info">future</span><span class="abc-name">Sprint 2</span></span>
            </div>
        </a>
        <a href="#" class="dropdown-item" role="button">
            <div class="period-title">
                <span><span class="badge badge-info">future</span><span class="abc-name">Sprint 3</span></span>
            </div>
        </a>
    </div>
</div>

and this is one of my tries that fails: (error message: "... is not a valid XPath expression)这是我失败的尝试之一:(错误消息:“...不是有效的 XPath 表达式)

driver.find_elements_by_xpath("//span[contains(text(), 'Sprint 1'")

Its failed due to syntax error.由于语法错误,它失败了。

Try this:尝试这个:

//span[contains(.,'Sprint 1')]

OR或者

//span/text()[contains(.,'Sprint 1')]

Thanks.谢谢。

To click on dynamic element you need to induce WebDriverWait () and wait for element_to_be_clickable () and following xpath.要点击动态元素,您需要诱导WebDriverWait () 并等待element_to_be_clickable () 并跟随 xpath。

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//span[text()='Sprint 1']"))).click()

You need to import below libraries.您需要导入以下库。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM