0
votes

I want to change pages like one by one, but can't access the element. http://prntscr.com/o0f4mx I tried everything but didn't work. Please I need help.

XPath = //*[@id="___gcse_0"]/div/div/div/div[5]/div[2]/div/div/div[2]/div[10]/div/div[2]

code:

tab2 = browser.find_element_by_xpath('//*[@id="___gcse_0"]/div/div/div/div[5]/div[2]/div/div/div[2]/div[10]/div/div[2]')
tab2.click()

And here is the error I get:

Traceback (most recent call last): File "C:/Users/......", line 38, in tab2 = browser.find_element_by_xpath('//[@id="___gcse_0"]/div/div/div/div[5]/div[2]/div/div/div[2]/div[10]/div/div[2]') File "C:\Users....\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath return self.find_element(by=By.XPATH, value=xpath) File "C:\Users...\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element 'value': value})['value'] File "C:\Users...\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Users....\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, 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="___gcse_0"]/div/div/div/div[5]/div[2]/div/div/div[2]/div[10]/div/div[2]"}

Update - correct element Correct Element

1
Sorry for showing the wrong element, here is right one: prntscr.com/o0f4mxSiyar
please approve the edits has been in pending they are necessaryakshay patil

1 Answers

0
votes

Use WebdriverWait and element_to_be_clickable and following xpath to handle dynamic element.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='gsc-cursor']//div[@class='gsc-cursor-page'][contains(.,'2')]")))
element.click()

If above code doesn't click on the element then try JS executor to click on the element.

element=WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//div[@class='gsc-cursor']//div[@class='gsc-cursor-page'][contains(.,'2')]")))
driver.execute_script("arguments[0].click();",element)