0

The html code is

<td>
    <i class="fas fa-arrow-down arrow-green"></i>
    <span class="fs_buy">Strong Buy</span> 
    1.11
</td>

if I use this code

cccss ='//*[@id="fs_title_values"]/div[3]/table/tbody/tr[1]/td[5]'
about = driver.find_element_by_xpath(cccss)
RatingCurrentValue=about.text
print ('RatingCurrentValue', RatingCurrentValue)

I will get all text: RatingCurrentValue Strong Buy 1.11

My goal is to get only the 1.11 without the text in the span tag.

Please, help me.

ventaquil
  • 2,780
  • 3
  • 23
  • 48

3 Answers3

0

To get the value 1.11 Use javascripts executor and get lastChild of the td element.

Induce WebDriverWait() and visibility_of_element_located()

element=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="fs_title_values"]/div[3]/table/tbody/tr[1]/td[5]')))
print(driver.execute_script('return arguments[0].lastChild.textContent;', element))

You need to add following libraries.

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

UPDATE:

print(driver.execute_script('return arguments[0].lastChild.textContent;', driver.find_element_by_xpath('//*[@id="fs_title_values"]/div[3]/table/tbody/tr[1]/td[5]')))
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • Thank you for your quick answer. But add 3 libraries for this simple task is very heavy. Is there a simplier method. – Eduard Bauer Jan 12 '20 at 12:17
  • @EduardBauer : This is the best practice to include wait to avoid any page sync problem in selenium.If you don't want use this just try updated one. – KunduK Jan 12 '20 at 12:52
0

You can remove the child node text from the full text to get parent node text.

cccss ='//*[@id="fs_title_values"]/div[3]/table/tbody/tr[1]/td[5]'


full_text = driver.find_element_by_xpath(cccss).text

child_text = driver.find_element_by_xpath(cccss + “//span”).text

parent_text = full_text.replace(child_text, '')
print(parent_text)
Muzzamil
  • 2,823
  • 2
  • 11
  • 23
0

The extract the text 1.11 from the element you can use the following based solution:

print(driver.find_element_by_xpath("//td[//span[@class='fs_buy' and text()='Strong Buy']]").get_attribute("innerHTML").splitlines()[2])

Ideally, you have to induce WebDriverWait for the visibility_of_element_located() and you can use the following XPATH based Locator Strategies:

print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//td[//span[@class='fs_buy' and text()='Strong Buy']]"))).get_attribute("innerHTML").splitlines()[2])

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
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352