[英]How to find title and list where data-id is highest number
I want to click the element with highest data-id.我想单击具有最高数据 ID 的元素。 I'm generating title like this:我正在生成这样的标题:
char_set = string.ascii_uppercase
tagTitle = "AI TAG " + ''.join(random.sample(char_set * 4, 4))
driver.find_element_by_xpath("//*[@id='FolderName']").send_keys(tagTitle)
Currently I'm getting all elements of UI class:目前我正在获取 UI 类的所有元素:
driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[1]/div[1]/div/div[2]/ul/li")
<ul class="investorGroup ul-groups"> <li data-id="-1" class=""> <a href="javascript:void(0)" onclick="$.InvestorContact.Group.LoadGroupInvestorsOnGroupClick(-1,null, 0)">Master</a> </li> <li title="AI TAG AOAI" data-id="371"> <a href="javascript:void(0)" onclick="$.InvestorContact.Group.LoadGroupInvestorsOnGroupClick(371)">2451b 24 (<span class="contactCatCount">0</span>)</a> <a href="javascript:$.InvestorContact.Group.OpenAddGroupModal(371)" class="edit"><i class="fa fa-pencil" aria-hidden="true"></i></a> </li> <li title="AI TAG CANG" data-id="376" > <a href="javascript:void(0)" onclick="$.InvestorContact.Group.LoadGroupInvestorsOnGroupClick(376)">452352 (<span class="contactCatCount">0</span>)</a> <a href="javascript:$.InvestorContact.Group.OpenAddGroupModal(376)" class="edit"><i class="fa fa-pencil" aria-hidden="true"></i></a> </li> </ul>
now tried and its showing the element :现在尝试并显示元素:
$x('/html/body/div 2 /div 2 /div 1 /div 1 /div/div 2 /ul/li[contains(@title,"AI TAG FOVE")]') $x('/html/body/div 2 /div 2 /div 1 /div 1 /div/div 2 /ul/li[contains(@title,"AI TAG FOVE")]')
but does not click and gives error via python:但不点击并通过python给出错误:
TagElement = driver.find_element_by_xpath('/html/body/div 2 /div 2 /div 1 /div 1 /div/div 2 /ul/li[contains(@title,"AI TAG FOVE")]') TagElement.click() TagElement = driver.find_element_by_xpath('/html/body/div 2 /div 2 /div 1 /div 1 /div/div 2 /ul/li[contains(@title,"AI TAG FOVE")]') TagElement.click ()
sorry if i skip something just a learner here.对不起,如果我在这里跳过一些只是一个学习者的东西。
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element ... is not clickable at point (122, 388). selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截:元素...在点 (122, 388) 处不可点击。 Other element would receive the click: ... (Session info: chrome=80.0.3987.132)其他元素将收到点击:...(会话信息:chrome=80.0.3987.132)
First you can get all the elements in a list and then you can click on the last element because that would be having the highest data-id
and if you want to get the title which you are clicking then you can get it by using get_attribute()
method.首先,您可以获取列表中的所有元素,然后您可以单击最后一个元素,因为这将具有最高的data-id
,如果您想获取正在单击的标题,则可以使用get_attribute()
方法。
You can do it like:你可以这样做:
# Fetching the elements using xpath
title_list = driver.find_elements_by_xpath("//ul[@class='investorGroup ul-groups']//li[contains(@title,'AI TAG')]")
# Getting the title of the last element
title_list[-1].get_attribute("title")
# Clicking on the last element
title_list[-1].click()
Edited ans for picking text from a variable:编辑 ans 以从变量中选取文本:
value = "AI TAG"
Now fetch the list by using:现在使用以下方法获取列表:
title_list = driver.find_elements_by_xpath("//ul[@class='investorGroup ul-groups']//li[contains(@title,"+value+")]")
First get all the <li>
tag in a list present under particular <ul>
.首先在特定<ul>
下的列表中获取所有<li>
标签。 Iterate through the loop and get compare and store highest data-id
attribute value element.遍历循环并获取比较并存储最高的data-id
属性值元素。 Finally click on that highest value element.最后点击那个最高值的元素。
Use below code :使用以下代码:
list_of_li = driver.find_elements_by_css_selector('.investorGroup.ul-groups li')
container = 0
number = None
for element in list_of_li:
number = element.get_attribute('data-id')
if container < int(number):
container = int(number)
max_element = element
print(container)
max_element.click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.