JS_executer script Error | Selenium Python Forum
G
Gaurav Valera Posted on 07/01/2021

Hi,

Please guide me to resolve the error while executing JS_executer script.

Page does not scroll and error 

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='intl_homepage1-zone-4']/div[4]/div[1]/h2"}
  (Session info: chrome=87.0.4280.88)

occurs.

Code:

import time
from selenium import webdriver

driver = webdriver.Chrome(executable_path='C:\\Git_Data\\automationFrameworks\\chromedriver.exe')
driver.maximize_window()
driver.implicitly_wait(10)

driver.execute_script("window.location= 'https://edition.cnn.com/'")
# load_status = driver.execute_script("return document.readyState")
# print(load_status)

i = 1
while i != 10:
    load_status = driver.execute_script("return document.readyState")
    print(load_status)
    if load_status == 'complete':
        break
    else:
        time.sleep(2)

i = 1
while i != 10:
    load_status = driver.execute_script("return jQuery.active")
    print(load_status)
    if load_status == 0:
        break
    else:
        time.sleep(2)


el = driver.find_element_by_xpath("//*[@id='intl_homepage1-zone-4']/div[4]/div[1]/h2")
print(el.location['x'])
print(el.location['y'])

# driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")

time.sleep(5)
driver.quit()

Output:

complete
0
Traceback (most recent call last):
File "C:/Git_Data/automationFrameworks/SelFinal/handlingJSE/jsExecuter.py", line 32, in <module>
el = driver.find_element_by_xpath("//*[@id='intl_homepage1-zone-4']/div[4]/div[1]/h2")
File "C:\Users\valergau\AppData\Local\Programs\Python\Python37\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\valergau\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\valergau\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\valergau\AppData\Local\Programs\Python\Python37\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='intl_homepage1-zone-4']/div[4]/div[1]/h2"}
(Session info: chrome=87.0.4280.88)

0
09914040666 Replied on 08/01/2021

Hey, 

The element to which you are trying to scroll to is not found by selenium. Kindly look for the locator again.