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")>

  • Possible duplicate of [Find next sibling element in Selenium, Python?](https://stackoverflow.com/questions/23887592/find-next-sibling-element-in-selenium-python) – birophilo Mar 09 '19 at 23:29
  • @birophilo: Not a dupe, elements of the same class are not necessarily siblings (and *vice versa*). – Kevin Mar 10 '19 at 19:08

3 Answers3

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
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • The problem is that the "4" thats showing the day is on a calendar, so where im trying to find it will change :/ – Will Mar 10 '19 at 00:59
0

Something like this maybe:

//span[text()="4"]/following-sibling::span
pguardiario
  • 53,827
  • 19
  • 119
  • 159
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]
Sers
  • 12,047
  • 2
  • 12
  • 31