Input box with drop down menu in Selenium and Python - 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()

Related

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 select a value from drop down menu in python selenium

I am writing a python script which will call a webpage and will select an option from the drop down to download that file. To do this task, I am using chropath. It is a browser extension which can give you the relative xpath or id for any button or field on the webpage and using that we can call it from python selenium script.
Above image shows the drop down menu in which I have to select 2019 as year and the download the file. In the lower part of the image, you can see that I have used chropath to get the relative xpath of the drop down menu which is //select[#id='rain']
Below is the code I am using:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("<URL>")
driver.maximize_window()
grbf = driver.find_element_by_xpath("//select[#id='rain']")
grbf.send_keys('2019')
grbf_btn = (By.XPATH, "//form[1]//input[1]")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(grbf_btn)).click()
from the above code, you can see that I am using xpath to select the drop down grbf = driver.find_element_by_xpath("//select[#id='rain']") and then sending keys as 2019 i.e. grbf.send_keys('2019') and after that I am calling download button to download it. But for some reason, its always selecting year 1999 from the drop down. I am not able to understand what is wrong in this. Is this correct approach to solve this. Please help. Thanks
I had the same problem time ago. Try this:
from selenium.webdriver.support.ui import Select
grbf = Select(driver.find_element_by_xpath("//select[#id='rain']"))
grbf.select_by_value('2019')
In the select_by_value() you have to use the value of the element in the dropdown.
By the way, if an element has id, use it.
grbf = Select(driver.find_element_by_id('rain'))
Try below code:
select = Select(driver.find_element_by_xpath("//select[#id='rain']"))
select.select_by_visible_text('2019')
Another approches to deal with dropdown:
Using Index of Dropdown :
select.select_by_index(Paass index)
Using valueof Dropdown :
select.select_by_value('value of element')
Using visible text of Dropdown :
select.select_by_visible_text('element_text')
In my opinion, I don't think this is the correct approach. You try to select the option which is dropdown (not a text box like ), so send key command does not work.
What you need to do is try to inspect HTML changing when clicking the dropdown and try to XPath for an option that you want to select.
If you still stuck at this problem, I recommend using katalon recorder which is a chrome extension to allow you to record and do UI testing

how to find id by class name python selenium

<input class="select_step ON " id="step_9285985_select" name="step_9285985_select" type="checkbox" value="9285985">
I am automating the regular tasks of our project by python selenium. struct at one step, Please help me.
As per the above html code id="step_9285985_select" randomly changing for every time browser opens through webdriver (python Selenium). I want to find the randomly generated "id" by the class="select_step ON or any other alternative.
First find the element by CSS selector:
input_tag = driver.find_element_by_css_selector('input.select_step.ON')
Then get the id attribute:
print(input_tag.get_attribute('id'))

Selenium: How to parse through a code after using selenium to click a dropdown

I'm trying to webscrape trough this webpage https://www.sigmaaldrich.com/. Up to now I have achieved for the code to use the requests method to use the search bar. After that, I want to look for the different prices of the compounds. The html code that includes the prices is not visible until the Price dropdown has been clicked. I have achieved that by using selenium to click all the dropdowns with the desired class. But after that, I do not know how to get the html code of the webpage that is generated after clicking the dropdowns and where the price is placed.
Here's my code so far:
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
#get the desired search terms by imput
name=input("Reagent: ")
CAS=input("CAS: ")
#search using the name of the compound
data_name= {'term':name, 'interface':'Product%20Name', 'N':'0+',
'mode':'mode%20matchpartialmax', 'lang':'es','region':'ES',
'focus':'product', 'N':'0%20220003048%20219853286%20219853112'}
#search using the CAS of the compound
data_CAS={'term':CAS, 'interface':'CAS%20No.', 'N':'0','mode':'partialmax',
'lang':'es', 'region':'ES', 'focus':'product'}
#get the link of the name search
r=requests.post("https://www.sigmaaldrich.com/catalog/search/", params=data_name.items())
#get the link of the CAS search
n=requests.post("https://www.sigmaaldrich.com/catalog/search/", params=data_CAS.items())
#use selenium to click in the dropdown(only for the name search)
driver=webdriver.Chrome(executable_path=r"C:\webdrivers\chromedriver.exe")
driver.get(r.url)
dropdown=driver.find_elements_by_class_name("expandArrow")
for arrow in dropdown:
arrow.click()
As I said, after this I need to find a way to get the html code after opening the dropdowns so that I can look for the price class. I have tried different things but I don't seem to get any working solution.
Thanks for your help.
You can try using the Selenium WebDriverWait. WebDriverWait
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(css));
First, You should use WebDriverWait as Austen had pointed out.
For your question try this:
from selenium import webdriver
driver=webdriver.Chrome(executable_path=r"C:\webdrivers\chromedriver.exe")
driver.get(r.url)
dropdown=driver.find_elements_by_class_name("expandArrow")
for arrow in dropdown:
arrow.click()
html_source = driver.page_source
print(html_source)
Hope this helps you!

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

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)

Categories