Reputation: 637
i am trying to play with autologin tests via selenium driver and python. I am using this site https://invoiceaccess.pgiconnect.com/ What i did:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://invoiceaccess.pgiconnect.com")
driver.find_element_by_id("LoginId").send_keys("test-account")
driver.find_element_by_id("LoginPassword").send_keys("test-password")
#driver.find_element_by_id("submit").click()
Everythyhing works, but i have a problem with selecting from drop-down menu. For example, i have html code of this menu.
<select class="regiondropdown" data-val="true" data-val-required="Please Select Region" id="Region" name="Region"><option value="">Select Region</option>
<option value="us">America</option>
<option value="europe">Europe</option>
<option value="apac">APAC</option>
</select>
I tried this:
element = driver.find_element_by_xpath("//select[@name='Region']")
all_options = element.find_elements_by_tag_name("option")
for option in all_options:
print("Value is: %s" % option.get_attribute("US"))
option.click()
For example, i need to select America
, but it selects APAC
. Where i made error, who can help me please ?
Upvotes: 1
Views: 563
Reputation: 33384
To select the value America from drop down box try this code.It worked for me.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver=webdriver.Chrome("Path of the Chrome driver" + "chromedriver.exe" )
driver.get("https://invoiceaccess.pgiconnect.com")
select =Select(driver.find_element_by_id("Region"))
select.select_by_value("us")
Upvotes: 2
Reputation: 555
Generic code for list using xpath and select
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://invoiceaccess.pgiconnect.com/")
driver.maximize_window()
def ListItemSelection(countrycode):
driver.find_element_by_xpath("//select/option[@value='" + countrycode + "']").click()
ListItemSelection("us")
time.sleep(1)
ListItemSelection("europe")
time.sleep(1)
ListItemSelection("apac")
time.sleep(1)
driver.quit()
Upvotes: 0
Reputation: 5443
To retrieve the specific option of your select
element which has us
for value you can use the Select
selenium class to do something like this :
from selenium.webdriver.support.ui import Select
option = Select(
driver.find_element_by_xpath("//select[@name='Region']")
).select_by_value("us")
print(option.text) # Should print 'America'
Or you can also do this with css selectors :
selec = driver.find_element_by_xpath("//select[@name='Region']")
option = selec.find_element_by_css_selector("option[value=\"us\"]")
print(option.text) # Should print 'America'
Upvotes: 1