1

I have list of li tag and in each li tag has some text with strong tag and normal text Xpath for //*[@id="main"]/li[1]/strong How do i get normal text, if i take xpath of li tag then it will scrape whole text, is there any way to get separate text

<li>
<strong>Heading</strong>
: Sample paragraph to get the text from here.
</li>
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
Palash
  • 11
  • 2
  • 2

3 Answers3

0

If you are using selenium Induce JavaScript Executor and get the lastChild of the node.

print(driver.execute_script('return arguments[0].lastChild.textContent;', driver.find_element_by_xpath('//*[@id="main"]/li[1]')))
KunduK
  • 26,790
  • 2
  • 10
  • 32
0

You can get the <li> text and remove the <strong> text from it

element = driver.find_element_by_xpath('//*[@id="main"]/li[1]')
all_text = element.text
element = element.find_element_by_xpath('./strong')
text = all_text.replace(element.text, '')
Guy
  • 34,831
  • 9
  • 31
  • 66
-1

To scrape the normal text you need to induce WebDriverWait for the visibility_of_element_located() and as the desired node is a text node you can use execute_script() method along with the following Locator Strategy:

  • xpath 1:

    print(driver.execute_script('return arguments[0].lastChild.textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//li[./strong[text()='Heading']]")))))
    
  • xpath 2:

    print(driver.execute_script('return arguments[0].lastChild.textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//li/strong[text()='Heading']/..")))))
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217