意外标记名称异常,同时单击下拉菜单项



我正在尝试让Python/Selenium正确单击下拉菜单并选择"IT",但我能做的最好的事情就是找到该元素并收到错误,指出我无法输入文本。

基本上,用户将单击该菜单并选择 IT 或键入 IT 并按回车键。

网页代码:

<div class="form-group">
<label class="col-md-3 control-label" for="id_tenant_group">Tenant group</label>
<div class="col-md-9">
<select name="tenant_group" nullable="true" class="netbox-select2-api form-control" data-url="/api/tenancy/tenant-groups/" data-filter-for-tenant="group_id" placeholder="None" id="id_tenant_group">
<option value="" selected>---------</option>
<option value="2">IT</option>
<option value="1">OT</option>
</select>

</div>

当我检查该元素时,我可以看到有一个 span 元素触发了一个事件,该事件显示另一个跨度,最终可以看到我的选项。

我无法通过可见文本进行选择,因为其他菜单也包含相同的"---------"可见文本。

我捕获了几张屏幕截图来说明该问题,希望对您有所帮助。 HTML 代码浏览器检查

老实说,我真的很迷茫,任何建议将不胜感激。

编辑: 我尝试了以下方法:

tenant_g_element = Select(browser.find_element(By.XPATH, '//span[@id="select2-id_tenant_group-container"]'))
tenant_g_element.selectByVisibleText("IT")

但是我有以下错误:

selenium.common.exceptions.UnexpectedTagNameException: Message: Select only works on <select> elements, not on span

你的第一行是错误的。它应该是这样的:

tenant_g_element = Select(browser.find_element(By.ID, 'id_tenant_group'))

因为您的属性 id="id_tenant_group"。没有必要使用 By.XPATH...如果你有理由需要它,那么你需要查找如何指定一个XPATH(请注意,//span将找到一个,而不是一个<选择>例如(。

此错误消息...

selenium.common.exceptions.UnexpectedTagNameException: Message: Select only works on elements, not on span

。意味着要传递给Select类的元素不是<select>节点,而是不支持的<span>元素。


似乎您的定位器正在识别<span>元素而不是<select>元素。因此,您会看到错误。此外,selectByVisibleText()不是有效的Python方法,而是需要使用select_by_visible_text()

溶液

要单击以文本作为IT的选项,您必须诱导WebDriverWait等待element_to_be_clickable(),您可以使用以下定位器策略之一:

  • 使用CSS_SELECTORselect_by_value()

    tenant_g_element = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select.netbox-select2-api.form-control#id_tenant_group[name='tenant_group']"))))
    tenant_g_element.select_by_value("2")
    
  • 使用XPATHselect_by_visible_text()

    tenant_g_element = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@class='netbox-select2-api form-control' and @id='id_tenant_group'][@name='tenant_group']"))))
    tenant_g_element.select_by_visible_text("IT")
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import Select
    

你的Xpath似乎错了。您正在尝试查找 span标记,但您应该找到选择元素。此外,您应该应用网络驱动程序等待select元素

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select

select = Select(WebDriverWait(driver, 50).until(
EC.presence_of_element_located((By.ID, "id_tenant_group"))))
WebDriverWait(driver, 50).until(
EC.presence_of_element_located((By.XPATH, "//select[@id='id_tenant_group']/option[2]")))
WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='id_tenant_group']//options[contains(.,'IT')]")))
select.select_by_visible_text('IT')

最新更新