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!
Related
I'm creating a webscraper with Selenium that will add products to a cart, and then cycle through cities, states and zip codes to give me the total cost of shipping + taxes for each area.
The website I'm using is: https://www.power-systems.com/shop/product/proelite-competition-kettlebell
Everything in my code appears to be working normally - the window will open, close a few pop ups and I can see Selenium select the proper option. However, regardless of whatever I've tried, after selenium clicks the "Add to Cart" button, it always adds the first option, despite having selected the proper one
Here is what I have been trying:
#created to simplify the code since I'll be using this option often
def element_present_click1(path_type,selector):
element_present = EC.element_to_be_clickable((path_type , selector))
WebDriverWait(driver, 30).until(element_present)
try:
driver.find_element(path_type , selector).click()
except:
clicker = driver.find_element(path_type , selector)
driver.execute_script("arguments[0].click();", clicker)
path = "C:\Program Files (x86)\msedgedriver.exe"
driver = webdriver.Edge(path)
driver.get('https://www.power-systems.com/shop/product/proelite-competition-kettlebell')
element_present_click1(By.CSS_SELECTOR,'button[name="bluecoreCloseButton"]')
element_present_click1(By.CSS_SELECTOR,'a[id="hs-eu-decline-button"]')
###this will correctly select the proper element
element_present_click1(By.XPATH, f"//span[text()='32 kg']")
###after this is clicked, it will always add the first option, which is 8 kg
driver.find_element(By.CSS_SELECTOR,'button.btn.btn-primary.add-to-cart.js-add-to-cart-button').submit()
I've tried a few different things, adding in some time.sleep() after clicking the option, refreshing the browser page, or selecting the option twice - no matter what I try, when I click add to cart it always adds the first option
Is there something I'm missing? Any help would be appreciated
You are using a wrong selector in the last step.
button.btn.btn-primary.add-to-cart.js-add-to-cart-button is not a unique locator.
You need to click the button inside the selected element block.
This will work:
driver.find_element(By.CSS_SELECTOR, ".variant-info.selected .add-to-cart-rollover").click()
It looks to me that find_element returns ONLY the first element it can find. Having taken a look at find_element it looks like you'd want to replace
driver.find_element(By.CSS_SELECTOR,'button...').submit()
with
random.choice(driver.find_elements(By.CSS_SELECTOR,'button...')).submit()
I try to click on dropdown options using selenium and python. The problem is that the dropdown values are different each time I open the browser therefore, i can not use the HTML tags from the webpage.
I'm wondering if there's any way of clicking the values?
I tried to use this line of code using answered provided by Sers in this case as an example How to select/click in a dropdown content using selenium chromewebdriver / python but I'm not sure how should I change/compose the class here.
att4 = WebDriverWait(driver, 2).until(ec.visibility_of_element_located((
By.CLASS_NAME, f"div.awsui-select-option awsui-select-option-selectable[title='{l}']"))).click()
l is for each option in my dropdown, I need to click on each of them
You are using a wrong, non-stable locator.
Try this:
att4 = WebDriverWait(driver, 2).until(ec.visibility_of_element_located((By.XPATH, "//div[#data-value='Bambino']"))).click()
Or
att4 = WebDriverWait(driver, 2).until(ec.visibility_of_element_located((By.XPATH, "//div[#title='Bambino']"))).click()
Hi I am not able to select drop-down option..
Sampe Dropdown : https://demos.telerik.com/kendo-ui/dropdownlist/angular
I wanted to select 'STATIC DAT' as a 'Austria' sung selenium python.
I am able to click on drop-down and all menus are visible but not able to select any option.
Sample code I rried:
xpath = r'/html/body/div[4]/div[2]/div[2]/div/div/span[1]/span/span[2]'
element_inst = web_driver.find_element_by_xpath(xpath)
# Click dropdown list
psu.perform_action(web_driver, element_inst, 'click')
# Select option
web_driver.find_element_by_xpath("//span[text()='Austria']").click()
Is there any solution ? Thanks
you can try the following sample.
dropdown = web_driver.find_element_by_xpath("//span[#class='k-widget k-dropdown']//span[text()='Albania']") // we depend on default country in list
dropdown.click() // trigger the dropdown
country = web_driver.find_element_by_xpath("//div[#class='k-list-scroller']/ul/li[text()='Austria']")
country.click() // select the country
or try just using the 'select' tag (not sure it will work due display: none)
selectCountry = web_driver.find_element_by_xpath("//select/option[#value='Austria']")
selectCountry.click();
if element cannot be selected by the normal way, try with jsclick
web_driver.execute_script("arguments[0].click();", selectCountry)
I am very new to web scraping, so I still have lots of trouble. Currently, I am trying to web scrape from https://www.enterprisetrucks.com/truckrental/en_US.html by setting the pickup time by running this code:
pickupTime = d.find_element_by_id('fldPickuptime_msdd')
pickupTime.click();
select = Select(d.find_element_by_id('fldPickuptime'))
select.select_by_value('20:00')
But I get the error saying that the element is not currently visible and may not be manipulated.
The dropdown which is present is not of the Select type, so you cannot use the Select method here. You need to click on the time using the xpath of that element directly.
You can use the xpath:
pickupTime = d.find_element_by_id("fldPickuptime_msdd")
pickupTime.click();
selectTime = d.find_element_by_xpath("//*[#id='fldPickuptime_msdd']//span[text()='8:00 PM']")
selectTime.click();
Code for JavaScriptExecutor Click:
element = driver.find_element_by_xpath("Enter the xpath here")
driver.execute_script("arguments[0].click();", element)
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)