Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i click an element using selenium from a long drop down list?

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() 
like image 218
Sohan Das Avatar asked Oct 24 '18 14:10

Sohan Das


People also ask

How do I select a dropdown in selenium?

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.”

How do I use select elements in selenium with webelement?

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.

What is click () method in selenium Python?

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.

How do I click a button in selenium?

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.


1 Answers

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() 
like image 112
monteua Avatar answered Oct 17 '22 20:10

monteua