0

If I have already searched for an element, is there any way I can find another in the same class?

ex.

<div class="day">
  <span class="day_number">4</span>
  <span class="day_item_time" data-day-total-time="day-total-time">10m</span></div>

So I had to search by the date (finding the element that had the 4), but how do I translate that to finding the 10m?

This was the result after printing out the element <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="4ff20f1a-75d1-42c1-a7d0-de0c532651a6", element="1147f2d5-8832-44d4-a906-929ebaaa49e2")>

2
  • Possible duplicate of Find next sibling element in Selenium, Python? Commented Mar 9, 2019 at 23:29
  • @birophilo: Not a dupe, elements of the same class are not necessarily siblings (and vice versa). Commented Mar 10, 2019 at 19:08

3 Answers 3

2

Try This below code.It should return your expected output.

driver.find_element_by_xpath("(//div[@class='day']/span)[1]").text
driver.find_element_by_xpath("(//div[@class='day']/span)[2]").text

Output:

4
10m
Sign up to request clarification or add additional context in comments.

1 Comment

The problem is that the "4" thats showing the day is on a calendar, so where im trying to find it will change :/
0

Something like this maybe:

//span[text()="4"]/following-sibling::span

Comments

0

You can get day-total-time using xpaths below.


Get span with day_number class and text 4, ancestor - get first parent div with class day and then get span with data-day-total-time attribute:

//span[@class='day_number' and .='4']/ancestor::div[@class='day'][1]/span[@data-day-total-time]

Get div with class day and child span with day_number class and text 4 and and then get span with data-day-total-time attribute:

//div[@class='day' and ./span[@class='day_number' and .='4']]/span[@data-day-total-time]

Comments

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.