How to choose available button to be selected on Appium Python - 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.

Related

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.

finding and clicking label , Selenium

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.

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 do I click a button in a form using Selenium and Python 2.7?

I am trying to create a Python program that periodically checks a website for a specific update. The site is secured and multiple clicks are required to get to the page that I want to monitor. Unfortunately, I am stuck trying to figure out how to click a specific button. Here is the button code:
<input type="button" class="bluebutton" name="manageAptm" value="Manage Interview Appointment" onclick="javascript:programAction('existingApplication', '0');">
I have tried numerous ways to access the button and always get "selenium.common.exceptions.NoSuchElementException:" error. The obvious approach to access the button is XPath and using the Chrome X-Path Helper tool, I get the following:
/html/body/form/table[#class='appgridbg mainContent']/tbody/tr/td[2]/div[#class='maincontainer']/div[#class='appcontent'][1]/table[#class='colorgrid']/tbody/tr[#class='gridItem']/td[6]/input[#class='bluebutton']
If I include the above as follows:
browser.find_element_by_xpath("/html/body/form/table[#class='appgridbg mainContent']/tbody/tr/td[2]/div[#class='maincontainer']/div[#class='appcontent'][1]/table[#class='colorgrid']/tbody/tr[#class='gridItem']/td[6]/input[#class='bluebutton']").submit()
I still get the NoSuchElementException error.
I am new to selenium and so there could be something obvious that I am missing; however, after much Googling, I have not found an obvious solution.
On another note, I have also tried find_element_by_name('manageAptm') and find_element_by_class_name('bluebutton') and both give the same error.
Can someone advise on how I can effectively click this button with Selenium?
Thank you!
To follow your attempts and the #har07's comment, the find_element_by_name('manageAptm') should work, but the element might not be immediately available and you may need to wait for it:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
manageAppointment = wait.until(EC.presence_of_element_located((By.NAME, "manageAptm")))
manageAppointment.click()
Also, check if the element is inside an iframe or not. If yes, you would need to switch into the context of it and only then issue the "find" command:
driver.switch_to.frame("frame_name_or_id")
driver.find_element_by_name('manageAptm').click()

Dependent DROP-DOWN selection using selenium and python

I have a form which has two drop-down menu say A and B. Contents of drop-down B is dependent on the selection of drop-down A.
The form uses AJAX to load the contents of drop down B.
I am using selenium and python to automatically select the drop down. I am able to select the drop down A but due to the use of AJAX my code is not working for selecting the content of drop-down B.
I have searched the selenium documentation (Explicit wait) and some stackoverflow answers but still I am unable to implement it in python. I am a newbie in python and selenium so please bear me.
Here is a small portion of my code :
#District selection DROP-DOWN A
district=Select(driver.find_element_by_id("ddlDistrict85"))
district.select_by_value("1")
#SRO selection DROP-DOWN B
# I Need EXPLICIT WAIT logic here to wait till the entire drop-down B is loaded
sro=Select(driver.find_element_by_id("ddlSRO85"))
sro.select_by_value("1")
Suggest some logic to wait till entire drop-down B is loaded.
You can use the options attribute from Select to check you have elements in the dropdown
district=Select(driver.find_element_by_id("ddlDistrict85"))
district.select_by_value("1")
sro=Select(driver.find_element_by_id("ddlSRO85"))
while len(sro.options) == 0:
continue
sro.select_by_value("1")
You haven't shared what the html looks like for these different menus, so let me assume that drop-down B is wrapped by a DIV with a specific class, or even better an ID, perhaps:
<div id="menuB"> ... </div>
Now, you could use Expected Conditions to wait for that menu to appear.
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.ID,'menuB')))
After searching a lot I found a simple solution where I am able to select the contents of drop-down B.
So I am answering my own question.
Use sleep function from time module to pause the execution of the program for some time(in seconds).
The program code would go like this :
import time #To import time module
#District selection
district=Select(driver.find_element_by_id("ddlDistrict85"))
district.select_by_value("1")
#SRO selection
time.sleep(5)
sro=Select(driver.find_element_by_id("ddlSRO85"))
sro.select_by_value("1")
It's working now.

Categories

Resources