当前位置: 首页 > 知识库问答 >
问题:

NoSuChelementException:消息:没有这样的元素:无法找到元素单击使用Selenium和Python的span元素

郎仰岳
2023-03-14
<div class="modal-content scrollbar">
  <div class="block block-always-show action-black-box waves-effect">
    <div class="icon xray-icon"></div>
    <span class="txt">Xray - Test Product Research</span>
</div>

我还在学习硒,但下面是我尝试过的:

driver.find_element_by_xpath("//span[contains(@class, txt) and contains(text()='Xray - Test Product Research')]").click()

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Xray - Test Product Research']"))).click()

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='txt' and contains(.,'Xray - Test Product Research')]"))).click()

我得到了这些错误:

NoSuchElementException: Message: no such element: Unable to locate element: 

而且

TimeoutException: Message: 

共有1个答案

萧树
2023-03-14

要单击带有文本的元素作为X射线测试产品研究,您必须引导WebDriverWait等待element_to_be_clickable(),您可以使用以下任一定位器策略:

>

  • 使用css_selector:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.modal-content.scrollbar span.txt"))).click()
    

    使用XPath:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='modal-content scrollbar']//span[@class='txt' and contains(., 'Xray - Test Product Research')]"))).click()
    

    注意:您必须添加以下导入:

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

    您可以在以下文件中找到关于NoSuchElementException的几个相关讨论:

      null

  •  类似资料: