1

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 ?

2
  • Do you want to retrieve the inner text of each option ? Or specifically the option which has "us" for value ? Commented Jan 8, 2019 at 22:27
  • @mgc i want to select US Commented Jan 8, 2019 at 22:30

3 Answers 3

2

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")
Sign up to request clarification or add additional context in comments.

Comments

1

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'

Comments

0

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.