Sourav
Sourav

Reputation: 91

Selenium Webdriver-python, unable to find the dynamic element, tried all the possible ways

I am successfully able to navigate to the desired page after logging in. Static elements are located and printed, peacefully. The page makes JavaScript call and the contents are updated after about 4-5 seconds, I'm unable to locate the dynamic elements.

I'm attaching the image of Inspect element before and after loading of Javascript elements.

Please, have a look at the code below and suggest the possible solution. P.S. Out of 100 times this worked worked for about 2-3 times.

layer = "https://desired.website"
driver.get(layer)
driver.find_element_by_id("email").send_keys('[email protected]')
driver.find_element_by_id("password").send_keys('myPassword')
driver.find_element_by_class_name("css-173kae7").click()

#NOW I'M SUCCESSFULLY LOGGED IN
#Opening the Desired Page, This is a static element
wait = WebDriverWait(driver, 30)
element = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "css-1nga9gi")))
driver.execute_script('''window.open("desired.page","_blank");''')

#Successfully opened desired page and switched to newly opened tab
#Trying to access the element present in <tbody> tag, please refer "Inspect Element after JavaScript elements are loaded"- image.
wait = WebDriverWait(driver, 30)
element = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "css-167dqxg")))
check = driver.find_elements_by_class_name("ccss-13ozsj7")
for e in check:
    print(e.text)
print("ALL DATA SUCCESSFULLY PRINTED")

Nothing happens for 30 seconds and I get time-out error and "ALL DATA SUCCESSFULY PRINTED" is displayed.

ERROR CODE I GET IS:

element = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "css-167dqxg")))
File "C:\Python\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

Please have a look at the Inspect element.

Inspect Element at the beginning of Page Load

Inspect Element after JavaScript elements are loaded

Upvotes: 1

Views: 337

Answers (2)

Scott Maretick
Scott Maretick

Reputation: 35

when using Python use either 'starts-with' instead of 'undefined-object'

EMAIL ADDRESS XPATH .//*[@id='email-undefined-objectObject-26149']

driver.find_element_by_xpath(".//input[starts-with(@id,'email')]".clear() driver.find_element_by_xpath(".//input[starts-with(@id,'email')]".send_keys("[email protected]")) ....or 'contains' driver.find_element_by_xpath(".//input[contains(@id,'email')]".clear()

Upvotes: 0

PixelEinstein
PixelEinstein

Reputation: 1733

You may not be switching to the new content on the tab opened, please try to add this:

#after this line
driver.execute_script('''window.open("desired.page","_blank");''')
#try adding this
#this will switch to the newest tab
driver.switch_to_window(driver.window_handles[-1])

You may be stuck looking for your desired elements on the old tab.

Upvotes: 1

Related Questions