Selenium in Python: find element after a click

811
January 03, 2017, at 01:06 AM

I need to build a find and click procedure, step by step, but I'm not able to do it.

My starting code is:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver=webdriver.Firefox()
driver.implicitly_wait(2)#seconds 
driver.get('http://mywebpage.com/page')
# The browser open page1
wait = WebDriverWait(driver, 2)
confirm = wait.until(EC.element_to_be_clickable((By.XPATH, "(//a[@class='button_serv'])[4]")))
confirm.click()
# The browser open page2 in the same window

Now I would like to find an element in page2, but the driver points always to page1 (because my code is wrong):

wait = WebDriverWait(driver, 2)
confirm = wait.until(EC.element_to_be_clickable((By.XPATH, "(//a[@class='button_serv'])[9]")))
confirm.click()

How to tell to selenium to wait and search fon content of page2?

Thank you

Answer 1

You should wait for the second page to load (assuming it is loaded by the confirm.click()). There are multiple ways you can do it. One of which is to wait for the URL to change checking the driver.current_url value inside the wait.until():

wait.until(lambda driver: driver.current_url != "http://mywebpage.com/page")

You can also wait for the "staleness" of the first "confirm" element:

Wait until an element is no longer attached to the DOM. element is the element to wait for. returns False if the element is still attached to the DOM, true otherwise.

wait.until(EC.staleness_of(confirm))

Note that there is no need to re-instantiate WebDriverWait() multiple times - do it once, after you define your driver instance.

READ ALSO
Python list error: [::-1] step on [:-1] slice

Python list error: [::-1] step on [:-1] slice

I thought I understood the basics of list slicing in python, but have been receiving an unexpected error while using a negative step on a slice, as follows:

494
Numpy's standard deviation method giving divide by zero error

Numpy's standard deviation method giving divide by zero error

I have written a function to regularise a set of features in a machine learning algorithmIt takes a rectangular 2D numpy array features and returns its regularised version reg_features (I am using the Boston housing prices data from Scikit-learn for training)

916
pyvenv returns non-zero exit status 1 (during the installation of pip stage)

pyvenv returns non-zero exit status 1 (during the installation of pip stage)

If you should ever encounter the following error when creating a Python virtual environment via the pyvenv command:

560
django postgres JsonField encoding

django postgres JsonField encoding

i have JSONField in a model this field store json in 'utf8' but when i am trying to get the value of this field it escaped

522