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()
Related
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()
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.
I'm having some problems in selecting values that are in a species of Dropdown Button. I've never seen a button that works in that way, it is equal to a dropdown menu, but it is classified in the website HTML as a button. So, selenium returns me an error when try to manipulate the button as if it were a menu.
Can you please help me to know what code should I run to select a value from the first dropdown menu of this Brazilian Central Bank website? The default value is REAL (BRL) and I want to use regular expressions to select the others.
edit:
df = pd.DataFrame();
selector = Select(driver.find_element_by_id("button-converter-de"))
options = selector.options
for index in range(0, len(options)-1):
df.append(pd.DataFrame.from_dict(eval(options[index])), ignore_index= True)
selector.select_by_index(df.loc[df.iloc[:,0].str.contains(str(moeda_origem))])
The error is:
"UnexpectedTagNameException: Select only works on select elements, not on button"
This page does not use default Select. Its dropdown is custom and, in order to work with it, do not use Selenium select and options, they won't work.
Try this instead:
driver = webdriver.Chrome()
driver.implicitly_wait(5)
driver.get('https://www.bcb.gov.br/conversao')
# Click to open the dropdown
driver.find_element_by_id("button-converter-de").click()
sleep(2) # Make sure dropdown opened
# Search for dropdown options by their selector
options = driver.find_elements_by_css_selector('#moedaBRL > li > a.dropdown-item')
print([o.text for o in options]) # this just prints all options, you can use your loop
I hope this helps, good luck!
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.
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)