我正在尝试通过selenium驱动程序和python进行自动登录测试。我正在使用此网站https://invoiceaccess.pgiconnect.com/我做了什么:
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()
一切都可以工作,但是我从下拉菜单中选择时遇到问题。例如,我有此菜单的html代码。
<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>
我尝试了这个:
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()
例如,我需要选择America
,但是它选择APAC
。我哪里出错了,谁能帮我?
要检索select
具有us
价值的元素的特定选项,可以使用Select
selenium类执行以下操作:
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'
或者,您也可以使用CSS选择器执行此操作:
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'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句