1

I am having this code

driver.implicitly_wait(300) # I have tried different timers
driver.find_element_by_xpath("(//button[@aria-pressed='false'])[1]").click()

But I get this error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"(//button[@aria-pressed='false'])[1]"}

However, this exists in my HTML content, like:

<div class="webix_el_box" style="width:90px; height:26px">
    <button aria-pressed="false" type="button" class="webix_img_btn" style="line-height:22px;">
        <span class="webix_icon_btn fa-lock red" style="max-width:22px;">
        </span> Read Only
    </button>
</div>

I have also tried to use the XPath expression:

//button[@class='webix_img_btn']//child::span[@class='webix_icon_btn fa-lock red']

Both XPath expressions worked fine in Google Chrome. In the website I am able to find the button using Ctrl + F and 'Read Only', can I use it in Selenium?


It is not working because of Selenium Driver is one page behind ajax call after large json data download.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jeevan
  • 447
  • 2
  • 8
  • 19
  • Edit: there are 3 of these buttons of almost same content, need to select three as per requirement and that is why [1] in my XPATH – Jeevan Jul 22 '20 at 16:46

2 Answers2

1

To click on the element with text as Read Only you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.webix_el_box > button.webix_img_btn"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='webix_el_box']/button[@class='webix_img_btn' and contains(., 'Read Only')]"))).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
    

References

You can find a couple of relevant discussions on NoSuchElementException in:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi DebanjanB, thank you so much for your reply. I get a timeout even during that time. For example when I try using XPath. Traceback (most recent call last): WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='webix_el_box']/button[@class='webix_img_btn' and contains(., 'Read Only')]"))).click() File "C:\Users\jrex\PycharmProjects\Dragon\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: – Jeevan Jul 23 '20 at 02:59
  • Hi @DebanjanB, I figured out what the issue is. I came to this page after clicking a button in another page. There is no change in URL, but I did print(driver.page_source) and it is still printing data from previous page. Not sure how to load this page. If you have any idea, please help. – Jeevan Jul 23 '20 at 05:22
  • Hi, I think it not working all because of this reason: https://stackoverflow.com/questions/63055456/selenium-driver-is-one-page-behind-ajax-call-after-large-json-data-download – Jeevan Jul 23 '20 at 17:57
0

Try doing just

driver.implicitly_wait(300)
driver.find_element_by_xpath("//button[@aria-pressed='false']").click()

This will click on the first element that satisfies the requirements.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aidan0626
  • 307
  • 3
  • 8
  • There are 3 of those XPATH in my HTML and I need to click the 1st one I tried it anyway selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@aria-pressed='false']"} – Jeevan Jul 22 '20 at 16:42