如何获得选择的选项(Selenium & Python)
How to get selected option (Selenium & Python)
我有一个问题,因为我想获得选定的选项名称:在本例中为 Option3。在这种情况下,我想使用断言来检查是否正确选择了值。您可以在下面看到我页面的一部分:
<html>
<body>
<table border="0" cellpadding="0" cellspacing="0" class="rich-toolbar " id="mainMenuToolbar" width="100%">
<tbody>
<tr valign="middle">
<td class="rich-toolbar-item " style=";">
<form id="substituteForm" name="name" method="post" action="http://homepage/home.seam" enctype="application/x-www-form-urlencoded">
<select name="substituteForm:j_id158" size="1" onchange="document.getElementById('substituteForm:substituteSubmit').click();">
<option value="0">Option0</option>
<option value="1">Option2</option>
<option value="2" selected="selected">Option3</option>
</select>
</form>
</td>
</tr>
</tbody>
</table>
</body>
</html>
我用DevTool复制了XPath,写了一段代码:
element = Select(driver.find_element_by_xpath("//* [@id='substituteForm']/select"))
我收到错误消息:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //*[@id='substituteForm']/select
我尝试了多种 XPath 组合,但仍然无法正常工作。
这似乎是时间问题,但不是 XPath
尝试使用下面的代码等待目标 select
元素出现在 DOM
:
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
select = wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//form[@id='substituteForm']/select")))
select.click()
selected_option = wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//option[@selected='selected']")))
assert selected_option.text == "Option3"
我有一个问题,因为我想获得选定的选项名称:在本例中为 Option3。在这种情况下,我想使用断言来检查是否正确选择了值。您可以在下面看到我页面的一部分:
<html>
<body>
<table border="0" cellpadding="0" cellspacing="0" class="rich-toolbar " id="mainMenuToolbar" width="100%">
<tbody>
<tr valign="middle">
<td class="rich-toolbar-item " style=";">
<form id="substituteForm" name="name" method="post" action="http://homepage/home.seam" enctype="application/x-www-form-urlencoded">
<select name="substituteForm:j_id158" size="1" onchange="document.getElementById('substituteForm:substituteSubmit').click();">
<option value="0">Option0</option>
<option value="1">Option2</option>
<option value="2" selected="selected">Option3</option>
</select>
</form>
</td>
</tr>
</tbody>
</table>
</body>
</html>
我用DevTool复制了XPath,写了一段代码:
element = Select(driver.find_element_by_xpath("//* [@id='substituteForm']/select"))
我收到错误消息:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //*[@id='substituteForm']/select
我尝试了多种 XPath 组合,但仍然无法正常工作。
这似乎是时间问题,但不是 XPath
尝试使用下面的代码等待目标 select
元素出现在 DOM
:
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
select = wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//form[@id='substituteForm']/select")))
select.click()
selected_option = wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//option[@selected='selected']")))
assert selected_option.text == "Option3"