Reputation: 17
Below is the HTML where i would like to do an if check for 'test', if exists I want to click on rtPlus
</div></li><li class="rtLI rtLast"><div class="rtBot rtSelected TreeNodeSelect sonetto-children">
<span class="rtSp"></span><span class="rtPlus"></span><span class="rtIn TreeNode sonetto-children">test</span>
my code
ul = driver.find_element_by_xpath("//div[@id='ctl00_ctl00_ContentPlaceHolderContent_MainCategoriserContent_Results1_ResultsTree1_radTree']/ul//ul")
ul = ul.find_element_by_xpath("./li[./div/span[text()='{}']]/ul//li[./div/span[text()='{}']]".format(levels[0],levels[1]))
if ul.text == 'test':
ul = ul.find_element_by_xpath("//span[@class='rtPlus']").click()
Hope this info is enough please leet me know what can be done here to click on the plus icon
Upvotes: 1
Views: 48
Reputation: 33384
You can use either of the xpath to accomplished your task.
Find span
tag with text as test and then use previous-sibling
.
driver.find_element_by_xpath("//span[text()='test']/preceding-sibling::span[@class='rtPlus']").click()
Or Find li
tag whose child
tag text as test
and find the next child
.
driver.find_element_by_xpath("//li[.//span[text()='test']]//span[@class='rtPlus']").click()
If you know the ul tag it is the same approach.
ul.find_element_by_xpath(".//span[text()='test']/preceding-sibling::span[@class='rtPlus']").click()
or
ul.find_element_by_xpath(".//li[.//span[text()='test']]//span[@class='rtPlus']").click()
Upvotes: 2
Reputation: 83527
when i run my code it opens the first plus sign on the page but I want it to open the plus sign next to test
It sounds like you are clicking on a different plus sign than the one you want. You need to write a more sophisticated algorithm to find the correct element to click on. My suggestion is something like this:
get the <ul> element
get all <li> elements inside the <ul>
for each <li>
if it has "test" in the text
click on it
So you need to learn how to get all of the elements that match a given css selector. Then you need to learn how to loop over those elements.
Upvotes: 2
Reputation: 4212
The primitive version of what you should do is:
ul = driver.find_element_by_xpath("text locator")
button = ul.find_element_by_xpath("button locator")
if ul.text == 'test':
button.click()
It's for the case if this button located inside your ul
element.
Upvotes: 0