Issue
I have an issue where I am running a while loop and sometimes the webpage reloads and other times it does not. If reloaded, then I have to press on show more to scroll down. I tried to do this by writing the following function.
def Press_Show_more(driver):
# time.sleep(1)
# Press once on the "show more" which will lead to infinit loop
path1 = "/html/body/div[1]/div/div[2]/studio-page/div/section/div/div/studio-video-results/video-results/div[3]/items-list/div/div[2]/div[2]/button"
el = Check_If_element_exist_by_path(driver, path1)
if 'WebElement' in str(type(el)):
el.click()
driver.find_element(By.TAG_NAME, 'body').send_keys(
Keys.CONTROL + Keys.HOME)
else:
print('The show more element does not exist')
This did not work well for me in the while loop. Any help? Is this the best way to write the code?
def Check_If_element_exist_by_path(driver, path2):
try:
el = WebDriverWait(driver, 1).until(
EC.presence_of_element_located((By.XPATH, path2)))
return el
except Exception as e:
print(f"The non-existant path: {path2}")
Solution
A simpler way to check for existence of an element is to use .find_elements()
and check for an empty list.
def Press_Show_more(driver):
path1 = "/html/body/div[1]/div/div[2]/studio-page/div/section/div/div/studio-video-results/video-results/div[3]/items-list/div/div[2]/div[2]/button"
el = WebDriverWait(driver, 1).until(EC.presence_of_all_elements_located((By.XPATH, path1)))
if len(el) > 0:
el[0].click()
driver.find_element(By.TAG_NAME, 'body').send_keys(
Keys.CONTROL + Keys.HOME)
else:
print('The show more element does not exist')
Since the check has been significantly simplified, you don't need the Check_If_element_exist_by_path()
method.
Answered By - JeffC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.