Home > Enterprise >  Why getting error Unable to locate element: {"method":"xpath","selector&quo
Why getting error Unable to locate element: {"method":"xpath","selector&quo

Time:11-23

I am new to python and selenium I have written a code in which at line getting error Message: no such element: Unable to locate element. even though it exist.

Code Segment:-

driver.find_element(By.XPATH, "//*[@id='react-joyride-step-0']/div/div/div/div[2]/div/button").click()

Full error log:-

DevTools listening on ws://127.0.0.1:57704/devtools/browser/2ecf6fa2-0489-46a9-b304-95cc40b4c2cc
Convin
logged in
Traceback (most recent call last):
  File "C:\Users\Lenovo\Desktop\convin\automation_test\logintest.py", line 38, in <module>
    driver.find_element(By.XPATH, "//*[@id='react-joyride-step-0']/div/div/div/div[2]/div/button").click()
  File "C:\Users\Lenovo\Desktop\convin\automation_test\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1238, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\Lenovo\Desktop\convin\automation_test\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 418, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Lenovo\Desktop\convin\automation_test\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='react-joyride-step-0']/div/div/div/div[2]/div
/button"}
  (Session info: chrome=95.0.4638.69)
Stacktrace:
Backtrace:
        Ordinal0 [0x00780C43 2493507]
        Ordinal0 [0x0071A4B1 2073777]
        Ordinal0 [0x00622608 1058312]
        Ordinal0 [0x0064CAA4 1231524]
        Ordinal0 [0x00676C62 1404002]
        Ordinal0 [0x0066597A 1333626]
        Ordinal0 [0x00675038 1396792]
        Ordinal0 [0x0066580B 1333259]
        Ordinal0 [0x00642314 1188628]
        Ordinal0 [0x0064316F 1192303]
        GetHandleVerifier [0x00907BF6 1548950]
        GetHandleVerifier [0x009B461C 2256060]
        GetHandleVerifier [0x0080C13B 518107]
        GetHandleVerifier [0x0080B1E0 514176]
        Ordinal0 [0x0071F53D 2094397]
        Ordinal0 [0x00723418 2110488]
        Ordinal0 [0x00723552 2110802]
        Ordinal0 [0x0072CE81 2150017]
        BaseThreadInitThunk [0x7602FA29 25]
        RtlGetAppContainerNamedObjectPath [0x77467A9E 286]
        RtlGetAppContainerNamedObjectPath [0x77467A6E 238]

PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C4800:29492:1123/114516.398:ERROR:chrome_browser_main_extra_parts_metrics.cc(230)] crbug.com/1216328: Checking Bluetooth availability s
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C ends.
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^Cn_extra_parts_metrics.cc(233)] crbug.com/1216328: Checking Bluetooth availability ended.
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^Cmpl.cc(214)] [11:45:16.398] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to th
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^Cn_extra_parts_metrics.cc(236)] crbug.com/1216328: Checking default browser status started. Please report if there is no report that th
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^Cmpl.cc(214)] [11:45:16.400] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to th
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^Cn_extra_parts_metrics.cc(240)] crbug.com/1216328: Checking default browser status ended.
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C
PS C:\Users\Lenovo\Desktop\convin\automation_test> ^C0360:29660:1123/114708.812:ERROR:gpu_init.cc(453)] Passthrough is not supported, GL is disabled, ANGLE is

developer console screenshot:-

developer console screenshot

Thanks in advance. Hope to here from you soon. I am really stuck please help.

CodePudding user response:

To click on the element with text as Skip you can use either of the following Locator Strategies:

  • Using css:

    driver.find_element(By.CSS_SELECTOR, "button[title='Skip'][aria-label='Skip'][data-action='skip']").click()
    
  • Using xpath:

    driver.find_element(By.XPATH, "//button[@title='Skip' and @aria-label='Skip'][@data-action='skip']").click()
    

Ideally to click on a clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='Skip'][aria-label='Skip'][data-action='skip']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@title='Skip' and @aria-label='Skip'][@data-action='skip']"))).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
    

CodePudding user response:

This works for me:

skip = driver.find_element(By.XPATH, "//*[@id='react-joyride-step-0']/div/div/div/div[2]/div/button")
driver.execute_script("arguments[0].click();", skip)
  • Related