Issue
I want Selenium to display a table with the maximum of rows. This is possible thanks to a button that allows to change the number of rows in the table. I have made a script :
url = 'http://www.side.developpement-durable.gouv.fr/EXPLOITATION/'
driver = webdriver.Chrome(executable_path=path_to_driver)
driver.maximize_window()
driver.get(url)
#I find the field where I want to execute my query
elem = driver.find_element_by_id("textfield")
#I write the text
elem.send_keys("photovoltaique")
#I send it
elem.send_keys(Keys.RETURN)
# I wait until ma page is loaded
WebDriverWait(driver, 15).until(EC.url_changes(url))
#I find the button to change my number of rows
elem = driver.find_elements_by_class_name("icon-arrow-bottom")
#I check the length of my object
print(len(elem))
#I click on the right button
elem[1].click()
This code is in a function called change_rows(url)
.
When I execute the script and call the function, this is what I receive as return :
- from the
print(len(elem))
: 1
When I execute manaully the instructions I get:
- from the
print(len(elem))
: 5
And I need the second element of my object elem
.
I tried with Firefox and Chrome webdriver.
Solution
If you want just change the page use:
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
url = 'http://www.side.developpement-durable.gouv.fr/EXPLOITATION/'
driver = webdriver.Chrome(executable_path=<Your driver page>)
wait = WebDriverWait(driver, 20)
driver.maximize_window()
driver.get(url)
# Find the query field, write text and press Enter
driver.find_element_by_id("textfield").send_keys("photovoltaique", Keys.RETURN)
# Wait and click on
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".taille_page .icon-arrow-bottom "))).click()
# Click on the last one, 50 for this case
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='taille_page']//li[last()]"))).click()
To get 5 elements, you need to wait for Javascript and jQuery loading completion:
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
url = 'http://www.side.developpement-durable.gouv.fr/EXPLOITATION/'
driver = webdriver.Chrome(executable_path=<Your driver page>)
wait = WebDriverWait(driver, 20)
driver.maximize_window()
driver.get(url)
# Find the query field, write text and press Enter
driver.find_element_by_id("textfield").send_keys("photovoltaique", Keys.RETURN)
# Wait for JavaScript and jQuery loading for completion
wait.until(lambda d: d.execute_script("return document.readyState === 'complete' && jQuery.active === 0;"))
# Wait for all elements to be visible
elem = wait.until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "icon-arrow-bottom")))
print(len(elem))
# Click on the right button
elem[1].click()
Answered By - Sers
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.