我必须测试用React编写的Web应用程序。我想从选择字段中选择选项:
HTML
<select id="martial_status" class="form-control" name="martial_status">
<option value="" hidden="">---</option>
<option value="1">wolny</option>
</select>
我正在尝试使用两种方法在“value”= 1上设置此字段:
martial_status = Select(driver.find_element(By.ID, "martial_status")
martial_status.select_by_value('1')
或
el = driver.find_element_by_id("martial_status")
for option in el.find_elements_by_tag_name('option'):
if option.text == 'wolny':
option.click()
break
它们都只适用于本地webdriver:
cls.driver = webdriver.Firefox()
cls.driver.implicitly_wait(20)
当我使用远程Webdriver时,未保存所选值:
cls.driver = webdriver.Remote(
command_executor='http://xx.xx.xx.xx:5555/wd/hub',
desired_capabilities=DesiredCapabilities.FIREFOX)
测试未检索到任何错误。从Selenium_standalone_server控制台启动远程服务器执行远程测试,我假设找到了选择:
14:31:16.993 INFO - Executing: [find element: By.id: martial_status])
14:31:17.002 INFO - Done: [find element: By.id: martial_status]
14:31:17.063 INFO - Executing: [tag name: 15 [[FirefoxDriver: Firefox on LINUX (661c3804-f7e8-470a-b516-95ad449efb8b)] -> id: martial_status]])
14:31:17.070 INFO - Done: [tag name: 15 [[FirefoxDriver: Firefox on LINUX (661c3804-f7e8-470a-b516-95ad449efb8b)] -> id: martial_status]]
14:31:17.130 INFO - Executing: [get element attribute: 15 [[FirefoxDriver: Firefox on LINUX (661c3804-f7e8-470a-b516-95ad449efb8b)] -> id: martial_status], multiple])
14:31:17.143 INFO - Done: [get element attribute: 15 [[FirefoxDriver: Firefox on LINUX (661c3804-f7e8-470a-b516-95ad449efb8b)] -> id: martial_status], multiple]
14:31:17.204 INFO - Executing: [find child elements: 15 [[FirefoxDriver: Firefox on LINUX (661c3804-f7e8-470a-b516-95ad449efb8b)] -> id: martial_status], By.cssSelector: option[value ="1"]])
14:31:17.212 INFO - Done: [find child elements: 15 [[FirefoxDriver: Firefox on LINUX (661c3804-f7e8-470a-b516-95ad449efb8b)] -> id: martial_status], By.cssSelector: option[value ="1"]]
14:31:17.272 INFO - Executing: [is selected: 16 [[[[FirefoxDriver: Firefox on LINUX (661c3804-f7e8-470a-b516-95ad449efb8b)] -> id: martial_status]] -> css selector: option[value ="1"]]])
14:31:17.278 INFO - Done: [is selected: 16 [[[[FirefoxDriver: Firefox on LINUX (661c3804-f7e8-470a-b516-95ad449efb8b)] -> id: martial_status]] -> css selector: option[value ="1"]]]
14:31:17.337 INFO - Executing: [click: 16 [[[[FirefoxDriver: Firefox on LINUX (661c3804-f7e8-470a-b516-95ad449efb8b)] -> id: martial_status]] -> css selector: option[value ="1"]]])
14:31:17.376 INFO - Done: [click: 16 [[[[FirefoxDriver: Firefox on LINUX (661c3804-f7e8-470a-b516-95ad449efb8b)] -> id: martial_status]] -> css selector: option[value ="1"]]]
但是在保存并重新输入后,我看到未设置选择。 相同的代码,本地webdriver和选定的字段都可以。 有谁解决这个问题的想法? :)
答案 0 :(得分:0)
您可以使用相对XPath来选择元素。
即(//option[@value='1'])