Home > database >  check if "show more" link has already been pressed python selenium
check if "show more" link has already been pressed python selenium

Time:08-21

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}")

CodePudding user response:

I don't know how you implemented the Check_If_element_exist_by_path method here, but I think you can do that with WebDriverWait ExpectedConditions to wait for element presence of visibility. Also, I'd suggest to use method returning a list of web elements so in case of presence of the desired element the returned list will be non-empty and will be interpreted by if as a Boolean True, otherwise it will return an empty list and it will be interpreted as a Boolean false. Like the following:

actions = ActionChains(driver)
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 = WebDriverWait(browser, 20).until(EC.presence_of_all_elements_located((By.XPATH, path1)))
    if el:
        actions.move_to_element(el[0]).perform()
        time.sleep(0.5)
        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')

You will need the following imports:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time

Also, You should improve your locators

CodePudding user response:

I would like to thank @Prophet who was helpful to enable me to reach this solution

def Press_Show_more(driver):
    # 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 = WebDriverWait(driver, 20).until(
        EC.presence_of_all_elements_located((By.XPATH, path1)))

    # Check if element is clickable
    try:
        el[0].click()
    except WebDriverException:
        pass
  • Related