Home > OS >  Failed to get only the "text" from "span" by selenium
Failed to get only the "text" from "span" by selenium

Time:10-07

this is my code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

service = Service("C:\Development\chromedriver.exe")
driver = webdriver.Chrome(service=service)

driver.get(
    "https://www.amazon.com/HB-UM43-רכזת-Sabrent-USB-3-0/dp/B00JX1ZS5O/ref=d_ex_efse_dp_ps_d_sccl_2_13/134-8388945-8876241?pd_rd_w=PhytY&content-id=amzn1.sym.a33bdad7-13b2-4641-a8b3-d6f6ef5a7304&pf_rd_p=a33bdad7-13b2-4641-a8b3-d6f6ef5a7304&pf_rd_r=7R3T4DW86KG7CHFAJHJH&pd_rd_wg=fWhFq&pd_rd_r=5bc9d4df-38d2-4d93-aaee-c6e8e63dcbf4&pd_rd_i=B00JX1ZS5O&th=1")

price = driver.find_element(By.CLASS_NAME, "a-offscreen")
print(price.get_attribute('innerHTML'))

driver.quit()

my results:

<span dir="rtl">$</span>‎16.98‎

site element:

<span ><span dir="rtl">$</span>‎16.98‎</span>

I want to get only the number "16.98", instead im getting the whole element. When im trying to write ".text" it returns "none".

CodePudding user response:

Instead .text use get_attribute('textContent') the element might be hidden on the page.

price = driver.find_element(By.CLASS_NAME, "a-offscreen")
print(price.get_attribute('textContent'))

This should return $16.98 to get the 16.98 use string manipulation like

print(price.get_attribute('textContent')[1:]) #this will remove `$`

CodePudding user response:

when .text is not working, also you could try to use .get_attribute('value')

  • Related