I'm trying to click on a element let's say a list of countries from a drop down list, but i'm able to click only first few countries using xpath, when i try to click the last country seems the click not working.Here is the code(it works for first few countries but i want to click the last country from the drop down list) If someone help me that would be appreciated!
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
import time
driver = webdriver.Chrome()
driver.get('https://www.example.com/dropdown')
##click accept cookies button
wait(driver, 5).until(EC.visibility_of_element_located(
(By.XPATH, '//div[@class="cookie-button-wrapper"]'))).click()
##time delay
time.sleep(20)
##click on specific country from the dropdown
wait(driver, 5).until(EC.visibility_of_element_located(
(By.XPATH, '//div[@class="tv-dropdown__button tv-dropdown-behavior__button tv-screener-market-select__button js-screener-market-button apply-common-tooltip common-tooltip-fixed"]'))).click()
wait(driver, 5).until(EC.visibility_of_element_located(
(By.XPATH, '//*[@data-market="argentina"]'))).click()
Declare the drop-down element as an instance of the Select class. In the example below, we named this instance as “drpCountry”. We can now start controlling “drpCountry” by using any of the available Select methods to select dropdown in Selenium. The sample code below will select the option “ANTARCTICA.”
Select elements can require quite a bit of boilerplate code to automate. To reduce this, and make your tests cleaner, there is a Select class in the Selenium support package. To use it, you will need the following import statement: You are then able to create a Select object using a WebElement that references a <select> element.
click () element method – Selenium Python Last Updated : 27 Apr, 2020 Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver.
Selenium offers a ‘.click ()’ method to help you perform various mouse-based operations for your web-application. You can use the Selenium click button method for various purposes such as selecting the radio button and checkbox or simply clicking on any button or link, drag and drop, click and hold, etc.
First try to scroll till element:
from selenium.webdriver.common.action_chains import ActionChains
element = driver.find_element_by_xpath("//*[@data-market='italy']")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
Then try to click on it, using the last part of your code:
wait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, '//*[@data-market="italy"]'))).click()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With