0
<div id="MainCopy_ctl13_presentJob_AddressLinesPanel">
    <div>
        2nd Floor
    </div>
    <div>
        1801 Morris Avenue
    </div>
    <div>
        Primary: 432567865
    </div>
</div>
  1. How can i get only 2nd floor
  2. How can i print only number(432567865)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
abhisekh
  • 31
  • 5

3 Answers3

0

element.text return the text of the element or first direct child.

You can use css selector, after getting the div with id.

element = driver.find_element_by_id(ID)
# This will get first div child
floor = driver.find_element_by_css_selector('#id > div:nth-child(1)')
second = driver.find_element_by_css_selector('#id > div:nth-child(2)')
# OR
childs = element.find_elements_by_tag_name('div')
# This will return all child divs in an array

You can also use element.find_element_by_css_selector(selector) to search within element (which we got by driver.find_element_by_id(ID))

While css selectors are faster to use, you can also use XPath if you like.

MoPo
  • 85
  • 1
  • 3
  • did not worked. i want to use on this try: agencyAddress = browser.find_element_by_id('MainCopy_ctl13_presentJob_AddressLinesPanel').text except: agencyAddress = 'No address' print(agencyAddress) – abhisekh Feb 15 '21 at 17:19
  • here i need to get nth child data – abhisekh Feb 15 '21 at 17:19
0
print(driver.find_element_by_xpath('//div[@id="MainCopy_ctl13_presentJob_AddressLinesPanel"]/div[contains(.,"Primary")]').split(':')[1])

use split and contains

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • did not worked i tried on try: phone = browser.find_element_by_id('MainCopy_ctl13_presentJob_Phone1Panel').text # phone = driver.find_element_by_xpath('//div[@id="MainCopy_ctl13_presentJob_Phone1Panel"]/div[contains(.,"Primary")]').split(':')[1] except: phone = 'No number' print(phone) – abhisekh Feb 15 '21 at 17:21
  • what did you get as output – PDHide Feb 15 '21 at 17:29
0

To print the text 2nd floor you can use either of the following Locator Strategies:

  • Using css_selector and get_attribute("innerHTML"):

    print(driver.find_element(By.CSS_SELECTOR, "div#MainCopy_ctl13_presentJob_AddressLinesPanel > div").get_attribute("innerHTML"))
    
  • Using xpath and text attribute:

    print(driver.find_element(By.XPATH, "//div[@id='MainCopy_ctl13_presentJob_AddressLinesPanel']/div").text)
    

To print the text 432567865 you can use either of the following Locator Strategies:

  • Using xpath:

    print(driver.find_element(By.XPATH, "//div[@id='MainCopy_ctl13_presentJob_AddressLinesPanel']//div[contains(., 'Primary')]").text.split(':')[1])
    

Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and text attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div#MainCopy_ctl13_presentJob_AddressLinesPanel > div"))).text)
    
  • Using XPATH and get_attribute("innerHTML"):

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='MainCopy_ctl13_presentJob_AddressLinesPanel']/div"))).get_attribute("innerHTML"))
    

To print the text 432567865 you can use either of the following Locator Strategies:

  • Using xpath:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='MainCopy_ctl13_presentJob_AddressLinesPanel']//div[contains(., 'Primary')]"))).text.split(':')[1])
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python


References

Link to useful documentation:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352