无法定位元素,并且使用Selenium和Python时没有此类元素错误

无法定位元素,并且使用Selenium和Python时没有此类元素错误,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,我想单击“客户详细信息”按钮,但出现错误。这是python中的一个错误: Message: no such element: Unable to locate element 我尝试了一些代码(如下所列),但都不起作用。有什么想法吗 1. driver.find_element_by_xpath("(//a[contains(text(),'Customer Details')])[11]").click() 2. driver.find_element_by_xpath("(//a[@hre

我想单击“客户详细信息”按钮,但出现错误。这是python中的一个错误:

Message: no such element: Unable to locate element
我尝试了一些代码(如下所列),但都不起作用。有什么想法吗

1. driver.find_element_by_xpath("(//a[contains(text(),'Customer Details')])[11]").click()
2. driver.find_element_by_xpath("(//a[@href='https://mylink' and @class=' class="btn-sm bg-navy btn-default"']").click()
3. driver.find_element_by_link_text("Customer Details").click()
这是我的HTML代码:

<table class="table table-bordered table-striped dataTable no-footer DTFC_Cloned" style="width: 100%; padding: 0px; margin: 0px;" role="grid" aria-describedby="tbl_so_info">
    <thead>
        <tr role="row" style="height: 0px;">
            <th class="sorting" tabindex="0" aria-controls="tbl_so" rowspan="1" colspan="1" aria-label=": activate to sort column ascending"></th>
            <th class="sorting_desc" tabindex="0" aria-controls="tbl_so" rowspan="1" colspan="1" aria-label="Customer No.: activate to sort column ascending" aria-sort="descending"></th>
        </tr>
        </thead>
        <tbody>
        <tr role="row" class="odd" data-dt-row="0" style="height: 38px;">
            <td data-dt-row="0" data-dt-column="0">
                <a href="https://mylink" onclick="window.open('https://mylink', '_blank'); return false;" class="btn-sm bg-navy btn-default" align="center">Customer Details</a>
                <a href="https://my_second_link" onclick="window.open('https://my_second_link', '_blank'); return false;" class="btn-sm bg-navy btn-default" align="center">Create Ticket</a>
            </td>
        </tr>
    </tbody>
</table>

使用
WebDriverWait
等待元素可单击,然后再单击它:

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

# ...
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'Customer Details'))).click()

# css selector
# wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[href="https://mylink"]'))).click()

使用
WebDriverWait
等待元素可单击,然后再单击它:

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

# ...
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'Customer Details'))).click()

# css selector
# wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[href="https://mylink"]'))).click()
要在元素上单击(),您需要引导WebDriverWait使
元素成为可单击的()
,您可以使用以下任一选项:

  • 使用
    链接文本

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Customer Details"))).click()
    
  • 使用
    CSS\u选择器

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn-sm.bg-navy.btn-default[href='https://mylink']"))).click()
    
  • 使用
    XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn-sm bg-navy btn-default' and @href='https://mylink'][contains(.,'Customer Details')]"))).click()
    
  • 注意:您必须添加以下导入:

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

参考文献 有关详细讨论,请参见:

单击()
在元素上,您需要引导WebDriverWait使
元素成为可单击的()
,并且您可以使用以下任一选项:

  • 使用
    链接文本

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Customer Details"))).click()
    
  • 使用
    CSS\u选择器

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn-sm.bg-navy.btn-default[href='https://mylink']"))).click()
    
  • 使用
    XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn-sm bg-navy btn-default' and @href='https://mylink'][contains(.,'Customer Details')]"))).click()
    
  • 注意:您必须添加以下导入:

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

参考文献 有关详细讨论,请参见:


在u添加[11]之前,您编写的xpath是正确的。 现在,您的代码正在搜索带有客户详细信息的a标记。但添加[11]将导致它在该元素中搜索代码中不存在的第11个结果。 因此它说没有发现这样的元素

试着只写下面的代码,这样就可以了

xpath=“//a[contains(text(),'Customer Details')”


注意:-切勿在定位器中使用这些([1][11][2]),这不是一个好方法,因为如果程序结构发生变化,那么定位器可能无法工作。

在添加[11]之前,您编写的xpath是正确的。 现在,您的代码正在搜索带有客户详细信息的a标记。但添加[11]将导致它在该元素中搜索代码中不存在的第11个结果。 因此它说没有发现这样的元素

试着只写下面的代码,这样就可以了

xpath=“//a[contains(text(),'Customer Details')”

注意:-切勿在定位器中使用这些([1][11][2]),这不是一个好方法,因为如果程序结构发生变化,定位器可能无法工作