Home > Software engineering >  Wait until the page is loaded/ element is visible (selenium)
Wait until the page is loaded/ element is visible (selenium)

Time:08-16

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

pwd = ''
gmail = ''
delay = 5

def open_driver(driver=None):
    if driver == None:
        driver = webdriver.Chrome(service = ChromeService(executable_path=ChromeDriverManager().install()))
   
        # Enter the search word
        driver.get("https://www.google.com/search?q=facebook")

        # Open the link
        driver.find_element(by=By.XPATH, value='/html/body/div[7]/div/div[10]/div[1]/div[1]/div[3]/div/div/div/div/div/div[1]/a/div[1]/span').click()
        
        # Already_have_an_account choice
        driver.find_element(by=By.XPATH, value='/html/body/div[1]/div[1]/div[1]/div[2]/div/div[2]/div/div/div[1]/form/div[1]/div[12]/a').click()

        # Click on the email box
        email_box = driver.find_element(by=By.XPATH, value='/html/body/div[1]/div[1]/div[1]/div/div[2]/div[2]/form/div/div[1]/input')
        email_box.click()
        # Enter the email
        email_box.send_keys(gmail)

        # Click on the password box
        pass_box = driver.find_element(by=By.XPATH, value='/html/body/div[1]/div[1]/div[1]/div/div[2]/div[2]/form/div/div[2]/div/div/input')
        pass_box.click()
        # Enter the password
        pass_box.send_keys(pwd)

        # Press the log in button
        driver.find_element(by=By.XPATH, value='/html/body/div[1]/div[1]/div[1]/div/div[2]/div[2]/form/div/div[3]/button').click()

        # Wait until the page is loaded
        # WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div/div[1]/div/div[2]/div[4]/div[1]/span/div/div[1]/div/svg/g/image')))
        

        # Press on the profile photo
        driver.find_element(by=By.XPATH, value='/html/body/div[1]/div/div[1]/div/div[2]/div[4]/div[1]/span/div/div[1]/div/svg').click()

        # Press on the Display & accessibility option
        driver.find_element(by=By.XPATH, value='/html/body/div[1]/div/div[1]/div/div[2]/div[4]/div[2]/div/div/div[1]/div[1]/div/div/div/div/div/div/div/div/div[1]/div/div/div[1]/div[2]/div/div[3]/div/div[1]/div[2]/div[1]/div/div/div/span').click()

        # Turn the dark mode ON
        driver.find_element(by=By.XPATH, value='/html/body/div[1]/div/div[1]/div/div[2]/div[4]/div[2]/div/div/div[1]/div[1]/div/div/div/div/div/div/div/div/div[2]/div/div[2]/div[2]/div/div[2]/label/div/div/div/div/div[1]/div/div[1]/div/div/div/div/span').click()
                
  
        driver.close()

The Web Driver Wait is not working, ad I want it to wait until the page is loaded, or until the specefied element is visible

And Here is the full code, I'm sorry if this question is silly but this is my first code with selenium. So hope you help me !

CodePudding user response:

You should handle SVG-namespace nodes not in the same way as HTML-namespace nodes. So instead of

/svg/g/image

you need

/*[name()="svg"]/*[name()="g"]/*[name()="image"]

CodePudding user response:

I would suggest you using WebDriverWaits before each action performed after each page changing.
Also, your locators should be improved.
Your code could be something like following:


wait = WebDriverWait(driver, 20)

driver.get("https://www.facebook.com/")

# fill email
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#email"))).send_keys(gmail)

# fill password
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#pass"))).send_keys(pwd)

# click login button
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[name='login']"))).click()

# click profile button
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div[aria-label="Account Controls and Settings"] svg[aria-label="Your profile"]'))).click()
  • Related