0

The following HTML code is as proceeds:

<div class="lower-text"> 
<div data-text="winScreen.yourCodeIs">Your Code Is:</div> 
<div>GENERATEDCODE</div>

What I'm trying to access is the second div node (the randomly generated number) in the class "lower-text"

 try:
    element = WebDriverWait(browser, 10).until(
        EC.visibility_of_element_located((By.CLASS_NAME, 'lower-text'))
    )
finally:
    found = browser.find_element_by_xpath("//a[@class='lower-text'][2]").text
    print(found)

This is what I've tried with no success. I am unsure as how to access the second div node. Any help would be appreciated.

immaeetu
  • 3
  • 2

1 Answers1

1

Please try below solution ::

element = WebDriverWait(driver, delay).until(
        EC.presence_of_element_located((By.XPATH, "//*[@data-text='winScreen.yournumberis']/following-sibling::div")))
    print element.text

Also you need to add below imports

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
SeleniumUser
  • 4,065
  • 2
  • 7
  • 30
  • Sorry, maybe I was not being clear enough. I am not looking for winScreen.yourNumberis. I am looking for the randomly generated number to appear. Appreciate the help though. – immaeetu Mar 26 '20 at 23:11
  • please check updated solution , Kindly accept answer and also hit upvote button once yor issue is resolved – – SeleniumUser Mar 26 '20 at 23:21
  • Unfortunately getting a timeout exception. This is the html code in better format. `div class="lower-text">
    Your Code Is:
    GENERATEDCODE
    ` Once again, I am trying to access the GENERATEDCODE node.
    – immaeetu Mar 26 '20 at 23:31
  • Increase your delay to WebDriverWait(driver, 30) – SeleniumUser Mar 26 '20 at 23:36
  • Got it. However it is now saying to text attributed to it. "Nonetype" has no attribute "text." – immaeetu Mar 26 '20 at 23:42
  • How manyy elements you are getting with above xpath if you are getting list of elements then change it to //*[@data-text='winScreen.yournumberis']/following-sibling::div[1] and try again – SeleniumUser Mar 26 '20 at 23:55
  • check if its present on a iframe ? – SeleniumUser Mar 27 '20 at 02:39
  • similar question asked again: https://stackoverflow.com/questions/60878849/printing-text-from-2nd-div-in-class-in-python-selenium/60880144#60880144 – Saurabh Mar 27 '20 at 04:52