简体   繁体   中英

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

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

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

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 = driver.find_elements(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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM