https://gyazo.com/b3e792b66775f64147671a6f23bd52c7
On the picture out from the first red dot, is there a list of options if u click on the "vælg" button (its in my language). I want my script to click on the right option.
Lets say the right one is "unisexur." How do I make my script click the option that says "unisexur" in the option list.
I know how to choose one of the options by e.g. xpath:
choice = browser.find_element_by_xpath('//*[#id="matrix-element-666"]/option[2]')
sleep(1)
choice.click()
which will make the code take the second option. So that is not what I want.
The html code is in the picture in the top..
You are probably looking for select_by_value.
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_id('matrix-element-666'))
select.select_by_value('unisexur').click()
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 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!
Go to google.com
Type a search keyword
I want to select 3rd/4th value from the auto suggestions list. What method should I use in selenium python ?
I don't know python, but i do have code in C# which i was able to succeed. You can give it try.
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("https://www.google.com/");
IWebElement txtboxSearch = driver.FindElement(By.Id("lst-ib"));
txtboxSearch.SendKeys("ap");
IList<IWebElement> autosaerchList = driver.FindElements(By.CssSelector(".sbsb_c.gsfs"));
autosaerchList[1].Click();
Google search page contains a <div class="gstl_0 sbdd_a">
When you start typing into the search box, that div becomes populated with an <ul role="listbox">. The <li> in that list contain the 4 suggestions. Pick one, and call the .click() method.
from selenium import webdriver
import time
driver = webdriver.Chrome('Path to chromedriver\chromedriver.exe')
driver.get('http://google.com')
driver.maximize_window()
driver.find_element_by_name('q').send_keys('Shah') #pass whatever you want to search
time.sleep(5)
# to click on third element of search suggestion
driver.find_element_by_xpath('//div/div[3]/form/div[2]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/li[3]/div/div[2]').click()
# to click on fourth element of search suggestion, uncomment the next line and comment the previous one
#driver.find_element_by_xpath('//div/div[3]/form/div[2]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/li[4]/div/div[2]').click()
Hope this help
I'm using Selenium and coding with Python.
I'm trying to do the following: for a flight search website, under Flight 1's 'Enter routing code' text box, type 'AA'
This is the code that I have at the moment:
flight1_routing = driver.find_element_by_xpath(".//*[#id='ita_form_location_RouteLanguageTextBox_0']")
flight1_routing.clear()
flight1_origin.send_keys("AA")
But instead, I get this error message: "invalid element state: Element is not currently interactable and may not be manipulated". How can this be with a regular text field that is also not an autocomplete field, AFAIK?
if you get Element is not currently interactable check if the element is not disabled and its visible. if you want to hack it execute JS to enable it.
i visited the homepage id ita_form_location_RouteLanguageTextBox_0 doesnt exist also under flight one there's no Enter routing code. i can see the text box saying airport city or city name
Also if you have the id prefer to use find_element_by_id if not try to use css selector if you can rather than xpath. Its much cleaner.
Update
here's a working script:
As recomended above, the elements selected are not visible. what is actualy done, is that there's 5-6 different elements all hidden and when you click on show advanced route it picks 2 random ones and makes them visible.
So the id is not always the same. If you use the same id you will get a hidden element some times(because it picks random ids) so selenium is not able to deal with it. i made a selector that gets the 2 hidden elements
from selenium import webdriver
import selenium.webdriver.support.ui as ui
driver = webdriver.Firefox()
driver.get("http://matrix.itasoftware.com/")
#click on the multi tab
tab = driver.find_element_by_id("ita_layout_TabContainer_0_tablist_ita_form_multislice_MultiSliceForm_0").click()
#click on the advanced routes
advanced_routing=ui.WebDriverWait(driver, 10).until(
lambda driver : driver.find_element_by_id("sites_matrix_layout_RouteLanguageToggleLink_1")
)
advanced_routing.click()
#get all visible elements with id like ita_form_location_RouteLanguageTextBox. its similar to regex ita_form_location_RouteLanguageTextBox.*
element = ui.WebDriverWait(driver, 10).until(
lambda driver : driver.find_elements_by_css_selector("[id*=ita_form_multislice_MultiSliceRow] [id*=ita_form_location_RouteLanguageTextBox]")
)
element[0].send_keys("foo")
element[1].send_keys("bar")
import time
time.sleep(20)
Did you click into the correct tab first & enable advanced routing codes?? e.g.
#Go to right tab
driver.find_element_by_css_selector("div#ta_layout_TabContainer_0_tablist_ita_form_multislice_MultiSliceForm_0 > span").click()
#Enable routing
driver.find_element_by_css_selector("a.itaToggleLink").click()
#note I seem to get a different id to the one you're using, assuming its dynamic numbering so handling all cases
#if you know how the dynamic numbering works youmay be able to deduce a single id that will work for your test case
#Instead I'm going for finding all elements matching a pattern then searching through them, assuming only one will be visible
flight1_routings = driver.find_elements_by_css_selector("input[id^='ita_form_location_RouteLanguageTextBox_']")
#probably better finding it then using it separately, but I was feeling lazy sorry.
for route in flight1_routings:
if route.is_displayed():
route.clear()
route.send_keys("AA")
Also you can probably skip the .clear() call as it looks like the box starts with no text to overwrite.
Edit: Updated the enable routing toggling to handle not knowing the id, the class name stays the same, should work. Handling finding the input despite variable id as suggested by foo bar with the css selector, just then iterating over that list and checking if its on top