finding and clicking label , Selenium - python

I want to click on an option in a drop down menu.
The options have label values.
The dropdown menu is not from a select element.
It is an input element with a drop down arrow next to it.
The dropdown arrow has the following attributes
<span id="ctl00_ContentPlaceHolder1_ReportViewer1_ctl09_ctl21_ctl01" class="glyphui glyphui-downarrow" style="cursor: pointer;"></span>
I managed to open the dropdown menu by clicking on the dropdown arrows by doing
Bedrijfsindeling_dropdown = driver.find_element_by_xpath('//span[#id="ctl00_ContentPlaceHolder1_ReportViewer1_ctl09_ctl21_ctl01"]')
Bedrijfsindeling_dropdown.click()
time.sleep(1)
I am not able to find a way to select any option.
The label looks like:
<label for="ctl00_ContentPlaceHolder1_ReportViewer1_ctl09_ctl21_divDropDown_ctl04">
Baggerbedrijf</label>
I want to be able to select for the "Baggerbedrijf" part.
Ultimately I want to select all options one by one, but for now it is sufficient to only be able to select "Baggerbedrijf"
I tried finding the label with driver.find_element_by_xpath('//label[#for="ctl00_ContentPlaceHolder1_ReportViewer1_ctl09_ctl21_divDropDown_ctl04"")']
and then clicking on it.
However, i get the "no such element" message.
How would i be able to select the option for "Baggerbedrijf"?

You need to wait for the element ("Baggerbedrijf") to be visible after you click the dropdown, then identify and click it. Otherwise Selenium will try and run off thorugh the script doing its thing, without waiting for the page DOM to (re)load :). So after you click the dropdown list, add this line.
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID,'ctl00_ContentPlaceHolder1_ReportViewer1_ctl09_ctl21_divDropDown_ctl04'))).click()
WebDriverWait requires these imports:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
It's good practice in general to use WebDriverWait. Consider using it to identify "Bedrijfsindeling_dropdown" as well, or any other webelement for that matter.

Related

python selenium: clicking buttons with various html element/css attributes

i'm trying to create python automation scripts using selenium to make appointment times, however i'm having some trouble with clicking on certain elements. to confirm an appointment the steps are going to the webpage > selecting the date > selecting the time > hitting confirm
this is what i've got so far:
driver = webdriver.Chrome()
driver.get('<url.date>') -- this works
driver.find_element(By.ID,'corresponding time id').click() -- this works
driver.find_element(By.DATA-TEST-ID,'order_summary_page-button-book').click() -- this is where i'm struggling
i've also tried doing it by the button class, using the css selector and looking for 'Reserve Now' text, and using the xpath
when i do it by xpath, i get the error NoSuchElementException
i tried adding a webdriver wait function but that didn't seem to work either.
the appointment times are really hard to get, so i'd rather not but any wait times into the code so it can execute and book as soon as slots open
here are the elements:
https://i.stack.imgur.com/S62kR.png
any help is appreciated!
Selenium doesn't have option to identify the element by DATA-TEST-ID locator.
In this case You have to use either by xpath or css selector
driver.find_element(By.XPATH,'//button[#data-test-id="order_summary_page-button-book"]').click()
OR
driver.find_element(By.CSS_SELECTOR,'button[data-test-id="order_summary_page-button-book"]').click()
Ideally you should use explicit wait to click on the element.
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//button[#data-test-id="order_summary_page-button-book"]'))).click()
you need following imports.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

How to choose available button to be selected on Appium Python

So I'm making a script to automate the test using appium python.
I have some buttons here as seen below:
The problem is, I want to select a button if the button is available and not fully booked.
I need to choose Date first (All the date buttons can be clicked), and then I'm able to choose the available time. When the button is available then I just click the button and the selecting process is over. I don't care which date or time button will be clicked as long as it can search the available one and click it.
I can make it static by choosing the available ones, but I want to make it automatically choose the available button.
I'm using the xpath like this "(//android.view.ViewGroup[#content-desc="date-day-btn"])[2]", and the number is like in an array, so there will be only 7 numbers (as for 1 to 7) for the date button and 6 numbers (as for 1 to 6) for the time button.
how can I make it possible to choose the button?
Please help, thanks!
From the UI screen example, I assume that available buttons are enabled, but not available buttons are disabled.
So this might help, it will find the first enabled element, found by XPATH:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
date_day_element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, '//android.view.ViewGroup[#content-desc="date-day-btn"]')))
date_day_element.click();
and something similar could be performed for the time button.

Python Selenium how to select a dropdown option in a google form

I'm having trouble to select the different options of this dropdown
in the field SIZE
https://docs.google.com/forms/d/e/1FAIpQLSdSpGLXjAV_wiI2qgg3B_KYxd4_7NR-DxHGrTySaIkAWIqmBg/viewform
Someone has a good solution?
Im able to click the element with
driver.find_element_by_xpath('/html/body/div/div[2]/form/div[2]/div/div[2]/div[14]/div/div/div[2]/div/div[1]/div[1]/div[1]').click()
But I'm not able to select the different options
You need to click the list to open it, then find and click the span with the right content inside it. Since there is a bit of drawing delay, you'll probably want it to use webdriverwait to ensure the element is ready before interacting.
This works for me:
driver = webdriver.Chrome()
driver.get("https://docs.google.com/forms/d/e/1FAIpQLSdSpGLXjAV_wiI2qgg3B_KYxd4_7NR-DxHGrTySaIkAWIqmBg/viewform")
#open the size menu
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='SIZE']/following::div[#class='quantumWizMenuPaperselectOptionList'][1]"))).click()
#select the size by it's full text
sizeToSelect = "US 11"
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(#class,'exportSelectPopup')]/div/span[text()='"+sizeToSelect+"']"))).click()
If you've not already got them you need the following imports:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Highlight the word SIZE and right-click and click inspect. Now do it again and it will lead you to the HTML regarding that. Keep going down from the root of that very HTML till you find the part of the size you want(as you hover over the code, it will highlight the relevant part in the website). Hope this will help you enough to make the code function.

Can't deselect a already selected blank space using selenium

I've written some code in python in combination with selenium to reach the target page where the data I'm after is located. My below code can almost get there. I just need a little twitch on it to make it work flawlessly.
Firstly, the browsers leads to a page where a default Login button located under Public User Login title then it clicks on that button. When a new page appears, It clicks on the Advanced tab located on the top of that page under the title of Account Search. Upon clicking on that tab there is a list of items visible under the title of Parcel Classification. Now, I need to choose 02.C - PROPERTY BURDENED BY CONSERVATION EASEMENTS from options and press the search button. That's it.
My scraper can do the whole thing accordingly but the problem is: when it selects the preferable option (I've mentioned the text above), there is another option (the very first blank space of those options) by default remains selected. That is why, when my scraper clicks on the search button, it populates wrong results (a page with no results).
How can I deselect the first blank space and continue on with the rest. Thanks in advance for taking a look into it.
The link to that webpage: Web_Link
This is what I've tried so far:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get("replace_with_above_link")
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#middle_left input[name='submit']"))).click()
wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Advanced"))).click()
Select(wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,".tableHtmlLayout #accountTypeID")))).select_by_visible_text('02.C - PROPERTY BURDENED BY CONSERVATION EASEMENTS')
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".buttons input[type='submit']"))).click()
driver.quit()
If i understand the problem correctly, you want to deselect the first option before you select your target option? How about doing something like this?
wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Advanced"))).click()
account_selector = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,".tableHtmlLayout #accountTypeID")))
Select(account_selector).deselect_by_index(0)
Select(account_selector).select_by_visible_text('02.C - PROPERTY BURDENED BY CONSERVATION EASEMENTS')
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".buttons input[type='submit']"))).click()

How to go on last page from number of pages using selenium python?

This is the Screenshot of my UI and HTML DOM.
I want to go to the last page one this I can do is pressing next button many times, but I don't know how many time it should be pressed, so i don't think so its a better way.
Can you suggest me better way using selenium commands.
I used this command (pressing next continuously):
driver.find_element_by_xpath('html/body/div[2]/div/section[8]/div/div/div[3]/div/div[2]/table/tfoot/tr/td/div/ul/li[5]/a').click()
You can use try/except like:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
while True:
try:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//li[not(#class="disabled")]/a[contains(text(), "Next")]'))).click()
except TimeoutException:
break
This should allow you to click Next button until last page (until button become disabled)
P.S. You should not use absolute XPath, e.g. html/body/div[2] ... /div/ul/li[5]/a as it's sensitive to changes in DOM. Use target element's attributes or its parent/child nodes to create relative XPath

Categories

Resources