UnexpectedTagNameException:消息:“选择"仅适用于< select>元素,而不是在< ul>上使用Selenium和Python选择下拉选项时出错 [英] UnexpectedTagNameException: Message: Select only works on <select> elements, not on <ul> error selecting an Dropdown option using Selenium and Python

查看:60
本文介绍了UnexpectedTagNameException:消息:“选择"仅适用于< select>元素,而不是在< ul>上使用Selenium和Python选择下拉选项时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是从Scopus网站上选择下图所示的四个子菜单(即,主题区域,标题,发布者,ISSN )之一,该菜单可通过以下链接访问:

用于搜索结果梳状下拉菜单的类名为 ui-menu ui-corner-bottom ui-widget ui-widget-content 的html代码段是

 < ul aria-hidden ="false" aria-labelledby ="srcResultComboDrp-button" id ="srcResultComboDrp-menu" role ="listbox"tabindex =" 0"class =" ui-menu ui-corner-bottom ui-widget ui-widget-content"aria-activedescendant =" ui-id-1"aria-disabled =" false"style =" width:251px;>< li class ="ui-menu-item">< div id ="ui-id-1" tabindex =-1" role ="option" class ="ui-menu-item-wrapper ui-state-active">主题区域</div></li>< li class ="ui-menu-item">< div id ="ui-id-2" tabindex =-1" role ="option" class ="ui-menu-item-wrapper">标题</div></li>< li class ="ui-menu-item">< div id ="ui-id-3" tabindex =-1" role ="option" class ="ui-menu-item-wrapper">发布者</div></li>< li class ="ui-menu-item">< div id ="ui-id-4" tabindex =-1" role ="option" class ="ui-menu-item-wrapper"> ISSN</div></li></ul>  

假设我们有兴趣选择子菜单 Title ,则可以按照 OP1 ,按以下几行;

从硒导入Webdriver的

 驱动程序= webdriver.Chrome(r"C:Browsers \ chromedriver.exe")url ='https://www.scopus.com/sources?zone=TopNavBar&origin=NO%20ORIGIN%20DEFINED'driver.get(网址)driver.find_element_by_xpath('//* [@ id =''ui-id-2'']').click() 

但是,编译器返回以下错误;

无法找到元素:{方法":"xpath",选择器":"//* [@ id =" ui-id-2"]"}

类似地,使用 OP2

建议的以下行从硒导入Webdriver的

 从selenium.webdriver.support.ui导入选择驱动程序= webdriver.Chrome(r"C:Browsers \ chromedriver.exe")url ='https://www.scopus.com/sources?zone=TopNavBar&origin=NO%20ORIGIN%20DEFINED'driver.get(网址)my_select =选择(driver.find_element_by_id('srcResultComboDrp-menu'))my_select.select_by_visible_text('标题') 

返回以下错误:

  selenium.common.exceptions.UnexpectedTagNameException:消息:Select仅适用于< select>元素,而不是在< ul>上 

我可以知道我在哪里做错了吗?感谢任何帮助

解决方案

要在主题区域标题 Publisher ISSN ,因为这些项目位于其父< li> 标记的子< div> 标记内 WebDriverWait 用于 element_to_be_clickable()和您可以使用以下定位器策略:

  • 使用 XPATH 选择标题:

      driver.get("https://www.scopus.com/sources?zone=TopNavBar&origin=NO%20ORIGIN%20DEFINED")WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,``//span [@ class ='ui-selectmenu-text'and text()='Subject area']'''''))))).点击()WebDriverWait(驱动程序,20).until(EC.element_to_be_clickable((By.XPATH,``//ul [@ id ='srcResultComboDrp-menu']//li [@ class ='ui-menu-item']/div[text()='Title']')))).click() 

  • 注意:您必须添加以下导入:

    从selenium.webdriver.support.ui中的

     导入WebDriverWait来自selenium.webdriver.common.by导入方式从selenium.webdriver.support导入EC的预期条件 


参考

您可以在以下位置找到几个相关的详细讨论:

The objective is to select either one of the four sub menus (i.e., Subject area, Title, Publisher, ISSN) as depicted in the picture below from the Scopus website that accessible via the link: https://www.scopus.com/sources?zone=TopNavBar&origin=NO%20ORIGIN%20DEFINED

The html snippet for the search result comb drop menu by the class name ui-menu ui-corner-bottom ui-widget ui-widget-content is

<ul aria-hidden="false" aria-labelledby="srcResultComboDrp-button" id="srcResultComboDrp-menu" role="listbox" tabindex="0" class="ui-menu ui-corner-bottom ui-widget ui-widget-content" aria-activedescendant="ui-id-1" aria-disabled="false" style="width: 251px;">
  <li class="ui-menu-item">
    <div id="ui-id-1" tabindex="-1" role="option" class="ui-menu-item-wrapper ui-state-active">Subject area</div>
  </li>
  <li class="ui-menu-item">
    <div id="ui-id-2" tabindex="-1" role="option" class="ui-menu-item-wrapper">Title</div>
  </li>
  <li class="ui-menu-item">
    <div id="ui-id-3" tabindex="-1" role="option" class="ui-menu-item-wrapper">Publisher</div>
  </li>
  <li class="ui-menu-item">
    <div id="ui-id-4" tabindex="-1" role="option" class="ui-menu-item-wrapper">ISSN</div>
  </li>
</ul>

Say we are interested to select the sub-menu Title, then the objective can be achieved as suggested by OP1, by the following lines;

from selenium import webdriver

driver = webdriver.Chrome(r"C:Browsers\chromedriver.exe")
url = 'https://www.scopus.com/sources?zone=TopNavBar&origin=NO%20ORIGIN%20DEFINED'
driver.get(url)
driver.find_element_by_xpath('//*[@id="ui-id-2"]').click()

However, the compiler return the following error;

Unable to locate element: {"method":"xpath","selector":"//*[@id="ui-id-2"]"}

Similarly, using the following line as suggested by OP2

from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome(r"C:Browsers\chromedriver.exe")
url = 'https://www.scopus.com/sources?zone=TopNavBar&origin=NO%20ORIGIN%20DEFINED'
driver.get(url)
my_select = Select(driver.find_element_by_id('srcResultComboDrp-menu'))
my_select.select_by_visible_text('Title')

Return the following error:

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

May I know where did I do wrong? Appreciate for any help

解决方案

To select either of the four sub menus among Subject area, Title, Publisher and ISSN as the items are within child <div> tags of their parent <li> tag, you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:

  • Selecting Title using XPATH:

    driver.get("https://www.scopus.com/sources?zone=TopNavBar&origin=NO%20ORIGIN%20DEFINED")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='ui-selectmenu-text' and text()='Subject area']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@id='srcResultComboDrp-menu']//li[@class='ui-menu-item']/div[text()='Title']"))).click()
    

  • Note : You have to add the following imports :

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


Reference

You can find a couple of relevant detailed discussions in:

这篇关于UnexpectedTagNameException:消息:“选择"仅适用于&lt; select&gt;元素,而不是在&lt; ul&gt;上使用Selenium和Python选择下拉选项时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆