1

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>

enter image description here

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

8
  • What is your question? What happens when you run this code? What do you want it to do instead? Commented Apr 13, 2021 at 15:18
  • 1
    Side note: don't reuse the same variable for multiple purposes. Instead, you can assign each thing to a new variable. Commented Apr 13, 2021 at 15:19
  • Add html code for the page or link to it Commented Apr 13, 2021 at 15:22
  • I don't see anything wrong with the code you posted here. I suggest that you do some debugging to make sure that you are selecting the elements you think you are. Commented Apr 13, 2021 at 15:25
  • 1
    @ArjunMunirathinam Please edit your question to add the description of what happens when you run your code. Your comment about clicking the wrong plus sign is an important detail that was not included in your original question. Commented Apr 13, 2021 at 15:30

3 Answers 3

2

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()
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0

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.

7 Comments

How is this different than the code that OP posted?
He has two ul variables and some problems with correctly defining locators.
I agree that using different variables is a good idea. However, this alone will not likely solve the OP's problem.
Yes, I am sure he has a problem with correctly defining locator
Yet your answer doesn't mention that nor gives any details about how to make the correct locator.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.