简体   繁体   English

如何点击<li>内的元素<ul>使用 Python3 和 Selenium 的列表框?</ul></li>

[英]How to click on <li> element within <ul> listbox using Python3 and Selenium?

I have an issue with navigating in the listbox made of UL list using Selenium 4.3 and Python 3. The problem is that I don't know how to switch value from default to any other and when I'm trying to use click method on those <li> elements I'm getting only errors.我在使用 Selenium 4.3 和 Python 3 在由 UL 列表组成的列表框中导航时遇到问题。问题是我不知道如何将值从默认值切换到任何其他值,以及当我尝试对这些值使用单击方法时<li>元素我得到的只是错误。

The screen shot of the html code below: html代码截图如下:

在此处输入图像描述

First I was trying to open whole list by clicking on it and after that choosing the element by the second click.首先,我试图通过单击它来打开整个列表,然后通过第二次单击来选择元素。 I was trying to access that by ID, like this:我试图通过 ID 访问它,如下所示:

driver.find_element(By.ID, "__list1").click()

But it was an error that the asked element doesn't exist.但是被问到的元素不存在是一个错误。

Do you have any idea how to navigate to different element from this kind of dropdown list?您知道如何从这种下拉列表导航到不同的元素吗? I know how to deal with this problem if the HTML tag would be a <select> .如果 HTML 标签是<select> ,我知道如何处理这个问题。

As per the HTML:根据 HTML:

快照

The element identified through (By.ID, "__list1") is a <ul> tag which are generally not clickable.通过(By.ID, "__list1")标识的元素是一个通常不可点击的<ul>标签。


Solution解决方案

Just like the usecase within the discussion you need to locate a <div> or a <span> element, which being clicked expands the <li> elements within either a <ul> or <ol> .就像讨论中的用例一样,您需要定位<div><span>元素,单击它会扩展<ul><ol>中的<li>元素。

Your effective code block will be:您的有效代码块将是:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "div_or_span_which_expands_li"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='sapUiSimpleFixFlexContent']/ul//li[text()='All Cases']"))).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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM