Reputation: 5
I want to click a button Customer Details, but i got an error. This is an error from python :
Message: no such element: Unable to locate element
I tried a few code(listed below) but they didn't work. Any ideas?
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()
An this is my HTML Code:
<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>
Upvotes: 0
Views: 363
Reputation: 193308
To click()
on the element you need to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies:
Using LINK_TEXT
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Customer Details"))).click()
Using CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn-sm.bg-navy.btn-default[href='https://mylink']"))).click()
Using 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()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You can find a relevant detailed discussion in:
Upvotes: 1
Reputation: 29
The xpath written by you was correct until u added [11]. Now your code is searching the a tag with Customer Details. But adding [11] will lead it to searching such element for 11th result which is not present in your code. Hence its says no such element found.
Try writing only the below code and it would work fine.
xpath = " //a[contains(text(),'Customer Details')] "
NOTE:- NEVER USE these ([1] [11] [2]) things in your locators it is not a good approach as if the structure of the program changes then the locator might not work.
Upvotes: 0
Reputation: 12255
Using WebDriverWait
wait for element to be clickable before click on it:
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()
Upvotes: 1