Selenium Hidden Elements: Hidden <option> Elements Within <select> - python

I am trying to select a particular option within a select box on a webpage. http://www.arnoldporter.com/news.cfm.
Specifically I want to selection "FDA and Healthcare" option within the Practice/Industry selection box.
I have tried a number of things, including clicking on the select tag, and then clicking on the FDA option. I have also checked to see whether the select tag changes after being clicked on. It doesn't.
So, Nothing seems to work, I keep getting the same error: Element is not currently visible and so may not be interacted with.
The xpath I am using for the select box is: //select[#class="medium" and #name="search_practice_id"]
The xpath I am using for the option is: //option[#value="323"]
There must be a simple solution, I just can't figure it out. Any help would be appreciated.

from selenium import webdriver
from selenium.webdriver.support.ui import Select
URL = 'http://www.arnoldporter.com/news.cfm'
CSS_SELECTOR = 'select[name=search_practice_id][class=medium]'
OPTION_TEXT = 'FDA and Healthcare'
browser = webdriver.Chrome()
browser.get(URL)
select_el = browser.find_element_by_css_selector(CSS_SELECTOR)
select = Select(select_el)
select.select_by_visible_text(OPTION_TEXT)

Related

how to select by a partial text in Python - selenium

How can I select a dropdown element by a part of its name?
I want to select an option based on a DB values, but this values don't have the complete name of the dropdown element, is there any way to make selenium look for the option with my database value as a partial text?
modelo = googleSheet.modelo.upper().strip()
select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div/div/div/div[1]/form/fieldset[6]/div/ul/fieldset[3]/div/ul/fieldset[3]/div/ul/fieldset/div/ul/li/label'))))
select.select_by_visible_text(modelo)
I the dropdown option I want to select is "Terrano II 2.7 xpto ol", but my data base value is just Terrano II 2.7
Thanks for the help
driver.select_by_visible_text() already does strip(). You do not need it.
Also, from this method definition:
Select all options that display text matching the argument. That is, when given "Bar" this would select an option like:
<option value="foo">Bar</option>
:Args:
- text - The visible text to match against
So, you need to expect exactly the option that is visible.
Another problem in your code is the way you pass the variable.
dropdown_option = "Some text you expect to see in the dropdown"
locator = driver.find_element_by_id("id") # or any other locator
select = Select(locator)
if locator is not None:
for option in select.options:
select.select_by_visible_text(dropdown_option)
This implementation makes debugging easier. For example you can print all the values from your dropdown before selecting the option you need.
If it takes a lot of time to dropdown to open, or other elements make your dropdown temporarily not visible, add a separate wait before selection.
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located(
(By.CSS_SELECTOR, "Unique css selector of the first option in dropdown")))
What about if first you extract the dropdown text content and then you check if your db query is in text? something like this:
Selenium Select - Selecting dropdown option by part of the text

Python Selenium- Cant find correct element. Login Button

#Thanks in advance for help. New to python, tried for hour trying to correct mistake.#
Trying to locate login button element. Attached is the image of the website with the element of the login button. please see here
Below is code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
url = "https://www.xxxxxx.com/index.php/admin/index"
username = 'xxxx'
password = 'xxxxx'
driver = webdriver.Firefox(executable_path="C:\\Users\\kk\\AppData\\Local\\Programs\\Python\\Python38-32\\geckodriver.exe")
driver.get(url)
driver.find_element_by_name(name='aname').send_keys(username)
driver.find_element_by_name(name='apass').send_keys(password)
driver.find_elements_by_xpath('//style[#type="submit"]')
Rather than finding it with a CSS selector. Why not use find_element_by_xpath()
To get the XPath of that element just right-click the HTML of the input in Inspect Element, hover over Copy and you'll see "Full XPath"
Issue is your xpath.
driver.find_elements_by_xpath('//style[#type="submit"]')
Use below:
driver.find_elements_by_xpath('//input[#type="submit"]')
or
driver.find_elements_by_xpath('//input[#value="login"]')
#This is more accurate as many input tags could have type as submit
Also, please use some sort of wait as i am not sure if page will be loading fast enough every time you launch URL.
You can identify the submit button by using any of these 2:
//input[#type="submit"] or //input[#value="login"]
They should work without any problem if you don't have any similar elements on your page (which I doubt)
But if you want to be more precise, you can mix these 2 into:
//input[#value="login" and #type="submit"]

how to get unique XPATH for buttons when the features for all input are the same

I am trying to extract NBA players stats using selenium web driver in python and here is my attempt:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
browser = webdriver.Chrome()
browser.get('https://www.basketball-reference.com')
xp_1 = "//select[#id='selector_0' and #name='team_val']"
team = Select(browser.find_element_by_xpath(xp_1))
team.select_by_visible_text('Golden State Warriors')
xp_2 = "//select[#id='selector_0' and #name='1']"
player = Select(browser.find_element_by_xpath(xp_2))
player.select_by_visible_text('Jordan Bell')
The problem I have is there are 4 "Go" buttons in this page and all have the same input features. In other words, the following xpath returns 4 buttons:
//input[#type='submit'and #name="go_button" and #id="go_button" and #value="Go!"]
I unsuccessfully tried adding ancestor as below but it does not return an xpath:
//input[#type='submit' and #name="go_button" and #id="go_button" and #value="Go!"]/ancestor::/form[#id='player_roster']
I appreciate any insight!
Try below XPAth to select required Go button:
"//input[#value='Go!' and ancestor::form[#id='player_roster']]"
or
"//form[#id='player_roster']//input[#value='Go!']"
Note that you shouldn't mix single and double quotes in a XPath expression and correct usage of ancestor axis is
//descendant_node/ancestor::ancestor_node
You could also switch to CSS selectors and use a descendant combination where you use an parent element to restrict to the appropriate form with Go button
#player_roster #go_button
That is
browser.find_element_by_css_selector("#player_roster #go_button")
The # is an id selector.
CSS selectors are generally faster than XPath, except in cases of older IE versions. More info.

Selecting an item from drop down menu when the menu uses input, not select

I'm trying to learn how to use Selenium with Python. I'm writing some code that will run a search on www.kijiji.ca. I'm able to select the search field and enter my query, but I can't figure out how to select the city from the list. In the Selenium documentation, I found where it says to use:
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_name('name'))
However, when I try to use the code, I get an error that says "Select only works on select elements, not on input"
I inspected the page again and it does seem like the drop down menu uses an input rather than a select. Can anyone help me figure out how to use Selenium to select the city here?
You can use Select class if your element is a <select> tag. Refer seleniumhq, there it mentionedModels a SELECT tag, providing helper methods to select and deselect options.
Specific to you element, you can try below code as manually we simulate the action like click.
driver.find_element_by_id("SearchLocationPicker").click()
driver.find_element_by_css_selector("li a[title='Manitoba']").click()
driver.find_element_by_css_selector("li a[title='Flin Flon']").click()
Working fine at my end. let me know if you have any query.
The page uses custom select component which cannot be interacted with selenium Select class. The Select class can be used only for default html select component.
We have to automate it as we do in the manual steps,
Click the input test box
Wait for the dropdown list appears.
Click the value respective value appears in the the dropdown.
In your case, we can select the dropdown either with title attribute or by selecting display value of the anchor tag in the list.
<a title="Territories">Territories</a>
def select_by_title(value):
# click the input component
driver.find_element_by_css_selector('input#SearchLocationPicker"]').send_keys(value)
# Wait for the value to appear in dropdown.
wait = WebDriverWait(driver, 60)
element = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '#SearchLocationPicker~div>ul>li>a[title="'+value+'"')))
# Click the element
element.click()
Or
def select_by_display_value(value):
driver.find_element_by_css_selector('#SearchLocationPicker"]').send_keys(value)
wait = WebDriverWait(driver, 60)
element = wait.until(EC.visibility_of_element_located((By.XPATH, '//input[#id="SearchLocationPicker"]/following-sibling::div/ul/li/a[text() = "'+value+'"]')))
element.click()

Input box with drop down menu in Selenium and Python

I am trying to use Selenium, python, and firefox to enter data into an input box on a website and select from a dropdown menu in the text box, but have been unable to do so. Formerly it was "just" a text box, but the website was changed.
The website is located at http://ecos.fws.gov/ecp/
the input textbox ID is "searchTerm". The site allows one to enter a scientific name (or part of a scientific name) and then select from options. For example, if you enter "Acipenser brevirostrum" into the textbox, it will provide you with a single option to click on.
Unfortunately, not sure how to code this up. Any help would be appreciated. So far I have this.
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.support.ui import Select
binary = FirefoxBinary(r'C:/Program Files (x86)/Mozilla Firefox/firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary)
driver.get("http://ecos.fws.gov/ecp")
SciName = driver.find_element_by_id('searchTerm')
SciName.send_keys(names)
SciName.send_keys(Keys.RETURN)
The last three lines used to work, but now that it is both an input box with a drop down menu, it fails.
Any help would be appreciated.
After you enter the name in the search box (id -- searchTerm), check for the visibility of the div (class -- autocomplete-suggestions) with an explicit wait. This contains the suggestions that the site throws up. Then you have to find the option which works for you. Try this xpath and click on the element. You should use findelements option as your text might not match anything and you will get an error.
"//div[#class='autocomplete-suggestion'][contains(.,'Acipenser brevirostrum')]"
If you have problems getting the autocomplete dropdown when you type the search text, send the text one character at a time with a small interval in between to trigger the call.
you can simply use driver.find_element_by_name,as html is:
<input type="text" class="form-control" name="query" placeholder="Search ECOS">
This code will work:
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://ecos.fws.gov/ecp/")
driver.find_element_by_name("query").click()
driver.find_element_by_name("query").send_keys("test")
driver.find_element_by_css_selector("button.btn.btn-default").click()

Categories