Select Element in Python Selenium for Dropdown Menu - python

I am new to Python and I am trying to use Selenium to select a value from a drop down menu on a Firefox browser.
This is what is am trying. Please let me know what I am doing wrong:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
browser = webdriver.Firefox()
browser.get('URL')
select = Select(browser.find_element_by_id('Yesterday'))
I need to add the following to my original post:
I have used Selenium IDE to try and identify the selector. Please image below. I would like to select "Yesterday" from the drop down list.
enter image description here

You can use like this, locate Select drop down then select the Custom value
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_name('locator of the select'))
select.select_by_value('Custom')
Also, you can use the index
select = Select(driver.find_element_by_name('locator of the select'))
select.select_by_index(6)

You can use browser.find_element_by_link_text("Yesterday") in place of browser.find_element_by_id. Since it does not contain an id tag it will not work.

Related

Webscaping table data with drop down menu help in either Pandas, Beautiful Soup or Selenium

I am trying to scrape data from this website:
https://www.shanghairanking.com/rankings/grsssd/2021
Initially pandas gets me out the gates and I can scrape the table but I am struggling with the drop down menus. I want to select the options next to the total score box which are PUB, CIT, etc. When I inspect the element it looks like maybe Javascript and the usual methods of interating over these options don't work. I have tried Beutifalsoup and most recently Selenium to select the drop downs by hand. This works for the default table data
'''
import time
import pandas as pd
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome('/Users/martinbell/Downloads/chromedriver')
driver.get('https://www.shanghairanking.com/rankings/grsssd/2021')
submit = driver.find_element_by_xpath("//input[#value='CIT']").click()
'''
Doesn't get me anywhere.
Your code would not work as you first have to click the dropdown open and then traverse through the options in the dropdown. Here is the refactored code.
Note that I have used time.sleep for instant purposes but for a robust code and good practice, use explicit wait such as WebdriverWait
driver.get('https://www.shanghairanking.com/rankings/grsssd/2021')
time.sleep(10)
driver.find_element(By.XPATH, "(//*[#class='inputWrapper'])[3]").click()
#The below commented code loops through all the dropdown options and performs actions.
# opt_ele = driver.find_elements(By.XPATH, "(//*[#class='rank-select'])[2]//*[#class='options']//li")
# for ele in opt_ele:
# print(ele.text)
# ele.click()
# print('perform your actions here')
# driver.find_element(By.XPATH, "(//*[#class='inputWrapper'])[3]").click()
# If you do not want to loop through but just want to select only CIT, here is the line:
driver.find_element(By.XPATH, "(//*[#class='rank-select'])[2]//*[#class='options']//li[text()='CIT']").click()

select country with python selenium

Click here for see the codeI'm learning python, but I don't know how to select this part, anyone help me?
I tried to select the cell with xpath and put the country but I don't know how to select it to assign the value
test = driver.find_element_by_xpath("//input[#type='search']")
test.send_keys('United States')
test.click()
What you want to do is to select the list item.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Firefox()
driver.get('url')
select = Select(driver.find_element_by_xpath("//input[#type='search']"))
# select by visible text
select.select_by_visible_text('United States')
# select by value
select.select_by_value('3')
Link to original answer](How to select a drop-down menu value with Selenium using Python?).
Try this.
# Assuming this this the correct xpath to the search bar
test = driver.find_element_by_xpath("//input[#type='search']")
test.click()
driver.find_elements_by_xpath("//li[contains(text(), 'United States') and #class='up-menu-item']").click()

Selenium Python options click

I want help in a little thing,
Look this:
I want to press the option named Boleto Bancario, but look the html
Than how I will press the second option with selenium PYTHON
Please Check the snippet.
You can select by value
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome(r'chromedriver.exe')
driver.get('url')
sct = Select(driver.find_element_by_id('idFormaPagamento'))
sct.select_by_value('813640')
You can select by index
sct.select_by_index('1')
You can select the value in any dropdown by three different ways:
select_by_value()
select_by_index()
select_by_visible_text()
So you may simply go and choose the option like this:
select_by_value('813640')
select_by_index('1')
select_by_visible_text('Boleto Bancario')
Hope this works for you.

Selenium Stops Extracting Option Text After Click Command

I am trying to use Selenium to extract dynamically loaded content. The content is on http://www.afl.com.au/stats
I am attempting to navigate to the 'Players' tab, then obtain a list of all the Seasons available. When I do this from the Teams tab, the following code works:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome(executable_path=r'D:\ChromeDriver\chromedriver.exe')
driver.get('http://www.afl.com.au/stats')
dropdown_menu = Select(driver.find_element_by_xpath('//*[#id="selTeamSeason"]'))
for option in dropdown_menu.options:
print(option.text)
which gives me a list of all the options available in the Seasons tab.
However, when I click to the 'Players' tab first, I am unable to get the same list with almost identical code:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
driver = webdriver.Chrome(executable_path=r'D:\ChromeDriver\chromedriver.exe')
driver.get('http://www.afl.com.au/stats')
driver.find_element_by_xpath('//*[#id="stats_tab"]/ul/li[2]').click()
time.sleep(3)
dropdown_menu = Select(driver.find_element_by_xpath('//*[#id="selTeamSeason"]'))
for option in dropdown_menu.options:
print(option.text)
The click successfully executes, I wait for the content to update, but instead of printing all the years (2001 to 2018), Selenium prints 18 instances of empty strings. I am thoroughly stumped. Any help at all would be appreciated.
In your first case , locator(//*[#id="selTeamSeason"]) is pointing to season dropdown of Teams tab and page has only one matching node at the time ,so its working for you.
But in the second case , for the same locator there are 2 matching nodes are available and in this case selenium automatically pick the first one(Its an hidden element in your case).
So try to build a unique xpath with can work in both the tabs.
You can try //div[#id='stats-player-stats']//select[#id='selTeamSeason'] locator for season dropdown in Players tab and //div[#id='stats-team-stats']//select[#id='selTeamSeason'] for season dropdown in Teams tab
Hope this will work for you
Instead of using Select just find the xpath and get all the option tags like below, tested and works.
element = driver.find_element_by_xpath('//*[#id="selTeamSeason"]')
all_options = element.find_elements_by_tag_name("option")
for option in all_options:
print(option.text)
In your first attempt the following Locator Strategy worked on the default TEAMS TAB :
dropdown_menu = Select(driver.find_element_by_xpath('//*[#id="selTeamSeason"]'))
As the first match as per the Locator Strategy was the Dropdown itself which is not the case when dealing with PLAYERS TAB. On PLAYERS TAB to list of all the options available in the Seasons Dropdown you can use the following code block :
dropdown_menu = Select(driver.find_element_by_xpath("//div[#id='stats-player-stats']//select[#id='selTeamSeason']"))
for option in dropdown_menu.options:
print(option.text)

Unable to click invisible select python

I am trying to retrieve all possible lists from this website
Model will only open when year is selected. Similarly Make will only open when Model is selected. I want to store all combination of Year, Model and Make for learning purpose.
However I am not able to click year field only. It seems it is hidden and without this I can't go ahead with rest of code.
from selenium import webdriver
import time
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup
driver = webdriver.Chrome()
driver.get("https://www.osram-americas.com/en-us/applications/automotive-lighting-systems/Pages/lrgmain.aspx")
# Switch to new window opened
driver.switch_to.window(driver.window_handles[-1])
# Close the new window
driver.close()
# Switch back to original browser (first window)
driver.switch_to.window(driver.window_handles[0])
el = driver.find_element_by_id('sbToggle_27562807')
el.click()
It gives error :-
Message: no such element: Unable to locate element: {"method":"id","selector":"sbToggle_27562807"}
Message: no such element: Unable to locate element: {"method":"id","selector":"sbToggle_27562807"}
year element is not hidden, actually your locator to locate element using find_element_by_id('sbToggle_27562807') is not correct. In this element id attribute value is dynamically changing that's why you're unable to locate this element.
Instead of id locator you should try using some different locator. I would suggest, for better way try using find_element_by_css_selector() as below :-
el = driver.find_element_by_css_selector("div#fldYear a.sbToggle")
el.click()
Use this CSS selector to get to the arrow pointing downwards on the select year dropdown
"div[id='fldYear'] > div[class='sbHolder'] > a[class='sbToggle']"
or this xpath
"//div[#id='fldYear']/div[#class='sbHolder']/a[#class='sbToggle']"
Click on this to webelement to get the options
The element IDs change on each reload of the page, you'll have to find a different way to find the dropdown.
You can always find the <a> link with the "-- Select Year --" text, for example.

Categories

Resources