我正在尝试自动检查表单的值。我想出了如何检查通过send_keys提交的值,但通过选择器提交的任何内容都给我带来了麻烦。在自动化方面,我是一个n00b。
这有效:
driver.find_element_by_xpath("//*[@name='CurrentAddress.LineOne'][@value='123 Something Lane']")
这不是:
driver.find_element_by_xpath("//*[@name='CurrentAddress.StateAbbrev'][@value='TX']")
管理CurrentAddress.StateAbbrev的代码是
<select class="form-control" data-val="true" data-val-required="* State is required" id="CurrentAddress_StateAbbrev" name="CurrentAddress.StateAbbrev"><option value=""></option>
当我尝试验证状态时,我明白了:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@name='CurrentAddress.StateAbbrev'][@value='TX']"}
表单值将输入存储为“TX”,但页面将State显示为“TEXAS”。我试过寻找,但它仍然找不到元素。试图通过@name或@id找到它并不会改变结果。
答案 0 :(得分:0)
我还没有测试过这段代码,但它应该可行。
//Rather than creating the object of WebElement create object of Select
Select stateDropdown = new Select(driver.findElement(By.xpath("//select[@class='form-control']"));
//To select by visible text, pass the text
stateDropdown.selectByVisibleText("");
//Get text of first selected option
String selectedValue = stateDropdown.getFirstSelectedOption().getText();
答案 1 :(得分:0)
这最终为我工作:
driver.find_element_by_xpath("//*[@class='form-control']/option[text()='TEXAS']")