How to interact with a non-select dropdown with selenium python - python

I am currently learning the use of selenium with python, and tried to collect some data. I have been struggling for the last couple days with clicking on a dropdown not accessible by Select method.
I looked at A LOT of questions on SOF, blogs, tutorials ... and could not find the answer to my problem.
The dropdown is accessible to this website <"https://en.volleyballworld.com/volleyball/competitions/olympics-2020/schedule/11349/">, then by clicking on the "Box Score" tab. Just below the teams flags, you'll see the dropdown with "ALL SETS" writing in it.
I would like to access the data from "SET 1", "SET 2", "SET 3". My guess would be to click on the dropdown, then click on "SET 1" and so on. But I couldn't make the code work to click on the dropdown.
Below is my code :
PATH = "C:\Program Files\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://en.volleyballworld.com/volleyball/competitions/olympics-2020/schedule/11349/")
#implicit wait to be sure the elements we want are loaded when we try accessing them
driver.implicitly_wait(5)
actions = ActionChains(driver)
#clicking on button
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "tab-title_boxscore")))
element.click() #mimic clicking on the clickable element
dropdown = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
(By.LINK_TEXT, "ALL SETS"))).click()
first_set = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
(By.LINK_TEXT, "SET 1"))).click()
Many thanks for your time and answer !

To click() on the element with text as ALL SETS and then to click on the element with text as SET 1 instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:
Code Block:
driver.get("https://en.volleyballworld.com/volleyball/competitions/olympics-2020/schedule/11349/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.tab-title_boxscore"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.tab-title_all"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='All Sets']//following::li[1]/a/span"))).click()
Browser Snapshot:

Related

Python selenium not detecting button

My selenium python script can't click the button with either click() or `driver.execute_script("arguments[0].click();", continue_). It worked for previous buttons but this particular button is only detected but can't be clicked.
Here is the code:
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(),'Continue')]")))
continue_ = driver.find_element(
By.XPATH, "//button[contains(text(),'Continue')]")
driver.execute_script("arguments[0].click();", continue_)
Update:
The answers didn't work for me. I tried adding prints to see where it stops.
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(),'Continue')]")))
print("Presence Located")
continue_ = driver.find_element(
By.XPATH, "//button[contains(text(),'Continue')]")
print("Continue Button Found")
driver.execute_script("arguments[0].click();", continue_)
print("Continue Button Clicked")
My console displays all prints till the "Continue Button Clicked" but is still not clicking the Continue so my bot can't progress through it's script. I don't know if it's any help but can't think of anything else.
Update
Tried using is_displayed() on the continue button it returned True.
The <button> element have the innerText spanned over multiple lines.
Solution
To click on the element instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using XPATH and starts-with():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#type='submit' and starts-with(., 'Continue')]"))).click()
Using XPATH and contains():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#type='submit' and contains(., 'Continue')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Select item inside a react-select dropdown list in Selenium with Python

Link example website here
In that website, I am looking how to select item (e.g "Green") in "Single" dropdown box and "Grouped" dropdown box.
I tried to click to dropdown first and tried to find the element inside it to click but I can't
Do you have any idea? and with Grouped dropdown I even couldn't find the xpath to click on it
Thank in advance
driver = webdriver.Chrome()
driver.get("https://react-select.com/home")
driver.maximize_window()
driver.implicitly_wait(20)
driver.find_element_by_xpath("//div[#class='select__value-container select__value-container--has-value css-1hwfws3']").click()
driver.find_element_by_xpath("//*[#text()='Green']").click()
remove # from text
driver.find_element_by_xpath("//*[text()='Green']").click()
To click on options that are not visible:
option=driver.find_element_by_xpath("//*[text()='Silver']")
driver.execute_script("arguments[0].scrollIntoView();", option)
option.click()
You have to scroll first to that and then click
To select Green from the Single dropdown box you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
driver.get("https://react-select.com/home")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.basic-single > div.select__control > div.select__value-container"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(#class, 'select__menu')]/div[contains(#class, 'select__menu-list')]//div[contains(#class, 'select__option') and text()='Green']"))).click()
Browser Snapshot:

How to click the Continue button within https://in.indeed.com/ website using selenium in python?

I am trying to write a code that is able to auto apply on job openings on indeed.com. I have managed to reach the last stage, however, the final click on the application form is giving me a lot of trouble. Please refer the page as below
Once logged in to my profile, I go to the relevant search page, click on the listing I am interested in and then on the final page (shown above) I am trying to click on the continue button using xpath. For the previous step (also a page on the same form), the form had multiple iframes which I was able to switch successfully. Now for the current page, I am stuck as the click function does not do anything. I have written the following code:
driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
driver.find_element_by_xpath('//*[#id="apply-button-container"]/div[1]/span[1]').click()
time.sleep(5)
frame_1 = driver.find_element_by_css_selector('iframe[title="Job application form container"')
driver.switch_to.frame(frame_1)
frame_2 = driver.find_element_by_css_selector('iframe[title="Job application form"]')
driver.switch_to.frame(frame_2)
continue_btn = driver.find_element_by_css_selector('#form-action-continue')
continue_btn.click()
driver.find_element_by_xpath('//*[#id="form-action-continue"]').click()
I have tried switching the iframes again for this step but nothing happens. Even the .click() function does not do anything.
Will appreciate some help on this.
The element Continue is within nested <iframe> elements so you have to:
Induce WebDriverWait for the parent frame to be available and switch to it.
Induce WebDriverWait for the child frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.indeed-apply-button-label"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[id^='indeedapply-modal-preload']")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='Job application form']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#form-action-continue"))).click()
Using XPATH:
driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='indeed-apply-button-label']"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#id, 'indeedapply-modal-preload')]")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='Job application form']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='form-action-continue']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

Sometime I can click on the button, sometime doesn't work

I am trying to click on a button if an element is found on the page. The element is on the page most of the time. 3 times work, and 1 time doesn't work.
Here is my code:
elements = driver.find_elements_by_xpath("//h2[contains(text(),'No results found')]")
if (len(elements)>0):
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CLASS_NAME, 'ut-navigation-button-control'))).click()
else:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Buy Now')]"))).click()
And below is the error I get sometime:
ElementClickInterceptedException: Message: Element <button class="ut-navigation-button-control"> is not clickable at point (128,80) because another element <div class="ut-click-shield showing interaction"> obscures it
try:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Buy Now')]"))).click()
except:
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CLASS_NAME, 'ut-navigation-button-control'))).click()
It's working like this, but it take a lot of time during the except. Does anyone know how to make it to go quickly through except?
The element with text as No results found would appear only after a unsuccessful search. On a successful search, to click on the desired element, you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following xpath based Locator Strategies:
try:
# wait for the visibility of the element with text as "No results found"
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[text()='No results found']")))
# if the element with text as No results found, induce WebDriverWait for invisibilityOfElement obscuring the clickable element
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("//div[#class='ut-click-shield showing interaction']")));
# once the invisibilityOfElement obscuring the clickable element is achieved, click on the desired element inducing WebDriverWait
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='ut-navigation-button-control']"))).click()
except TimeoutException:
# if search for the element with text as "No results found" raises "TimeoutException" exception click on the element with text as "Buy Now" inducing WebDriverWait
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(,. 'Buy Now')]"))).click()
You can perform JavaScriptExecutor click on the element as it directly performs the action on the div and is not affected by the position of the element on the page.
You can do it like:
button = driver.find_element_by_xpath("//button[contains(text(),'Back')]")
driver.execute_script("arguments[0].click();", button)

How to close a window using selenium in python with html as follows

I am using selenium in python to try to close a window, but the close button has html as follows, so what shall my driver selects to close it?
<div class="close-button-container">Close</div>
Try this..
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("your_link")
elem = driver.find_element_by_xpath("//a[#class='overlay-panel-close fg-close-tab-gtm']")
elem.click()
The desired element is within a Overlay Panel so you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:
LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Close"))).click()
CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.close-button-container>a.overlay-panel-close.fg-close-tab-gtm"))).click()
XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='overlay-panel-close fg-close-tab-gtm' and contains(.,'Close')]"))).click()

Categories

Resources