Webdriver。如何检索找到的元素的xpath? [英] Webdriver. How to retrieve the xpath of found element?
问题描述
我有一些代码可以根据可见文本查找元素。
I have a bit of code that finds an element based on visible text.
# Defines the text I am looking for
productName = "customer x job"
#Finds the element I am looking for
inputElement = driver.find_element_by_xpath(".//*[@id='demo_top']/div/table[2]/tbody/tr/td[1]/ul/li[2]/ul/li[1]/a".format(productName))
我需要点击右侧的方框。
I need to click a box just to the right of this.
元素的特定xpath是:
The specific xpath of the element is:
#What I just found
/html/body/div[1]/div/div/table[2]/tbody/tr/td[1]/ul/li[2]/ul/li[1]/a
#The box I actually want to click
/html/body/div[1]/div/div/table[2]/tbody/tr/td[1]/ul/li[2]/ul/li[1]/span/a
所以我只需要添加 / span / a 到inputElement的xpath。问题是,我不知道该元素的特定xpath。有没有办法将inputElement转换为特定的xpath,所以我可以在其末尾附加 / span / a并单击该元素?
So I just need to add "/span/a" to the xpath of inputElement. The issue is, I don't know the specific xpath of this element. Is there a way for me to convert inputElement to a specific xpath, so I can just append "/span/a" to the end of it and click that element?
编辑:似乎我弄错了,我需要用 span / a替换最后的 a
seems I was mistaken, I need to replace the final "a" with "span/a"
下面是html:
<li class="TCbullet">
<span>
<a class="editCategory " onclick="return hs.htmlExpand(this, { objectType: 'iframe' } );" href="cat-frame-category.php?categoryID=1340"> </a> #I want to click this
<span class="sort">sort: 0</span>
</span>
<a class="anchorCategory" alt="Eden Cognitive Content" href="?ownerID=C518&g=1085&t=1340">Eden Cognitive Content</a> # I have found this
</li>
我要访问editCatagory链接,并且找到了anchorCategory链接。
I want to access the editCatagory link, and I have found the anchorCategory link.
推荐答案
您可以致电 find_element_by_xpath()
在您已经找到的元素上:
You can call find_element_by_xpath()
on the element you have already found:
link = inputElement.find_element_by_xpath('./preceding-sibling::span/a')
这将选择 span <中的
a
标签/ code>跟随您找到的 a
标记。
This would select the a
tag inside the span
that is following the a
tag you have found.
这篇关于Webdriver。如何检索找到的元素的xpath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!