Hi I am trying to use selenium to select an item from a dropdown using the actual text not the value option. My script pulls states out of a list and iterates through the states selecting the dropdown that matches the list.
When I try the following code it throws out an error:
for agentinfo1, agentstate1 in zip(agentinfo, agentstate):
select2 = driver.find_element_by_css_selector("#selst" %agentstate1)
select2.click()
select2 = driver.find_element_by_css_selector("#selst" %agentstate1)
TypeError: not all arguments converted during string formatting
I'm wondering if the error is thrown out becuase when I grab the data that I put in my list I append a "\n" but even when I take out that code it does not work.
you are using css IDs instead of CSS classes. select2 is pointer to the select element, if your text is exactly the same as in your variable
from selenium.webdriver.support.ui import Select
for agentinfo1, agentstate1 in zip(agentinfo, agentstate):
select2 = Select(self.driver.find_element_by_id("<some id of the select element>")).select_by_visible_text(agentstate)
if it is only partial match you can try this (case insensitive partial match):
from selenium.webdriver.support.ui import Select
for agentinfo1, agentstate1 in zip(agentinfo, agentstate):
select2 = Select(self.driver.find_element_by_id("<some id of the select element>"))
for each_option in select2.options:
if agentstate.lower in each_option.text.lower:
select2.select_by_index(int(each_option.get_attribute("value")))
Related
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
I am trying to select and enter a string into a search bar however I receive a typeError: "str" object is not callable
ive tried selecting the element by ID, xpath, and Css selector to no avail. Ive also tried clicking the search bar after selecting it before entering the string which was also not effective.
driver.get("https://www.gsmarena.com")
searchBar = driver.find_element(By.CSS_SELECTOR('#topsearch-text'))
searchBar.send_keys("iphone")
I want to be able to insert a string into the search bar and hit enter which should take me to the next page that contains a list of the phones that I am searching for.
selecting the element by input name and use Keys.ENTER to search iphone.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("")
driver.get("https://www.gsmarena.com")
searchBar = driver.find_element_by_name('sSearch')
searchBar.send_keys("iphone")
searchBar.send_keys(Keys.ENTER)
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.
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()
I am dealing with drop-downs that were created with Angular JS. When I look at HTML, the drop-down options that I see have a tag and not a standard . So when I try to figure out the drop-down length, the following standard approach doesn't work
select = drowser.find_element_by_id(elementID))
print len(select.options)
How else can I try to find out how many options my drop-down has?
EDITED: Below is an example of HTML:
Your element is not a HTML select tag and it's not possible to use Selenium's Select class on it. Instead get all elements with tag mat-option under div.mat-select-content:
selectOptions = drowser.find_elements_by_css_selector("div.mat-select-content mat-option")
print len(selectOptions)