I want to get text from span class using selenium webdriver below is the code which I have tried but no result
Output should: Marketplace listings – 72
Code trials:
driver.find_element(By.XPATH,"//h2[@class='jxuftiz4 jwegzro5 hl4rid49 icdlwmnq gvxzyvdx aeinzg81']//span").get_attribute("innerText")
Here is the html
<div >
<div >
<div >
<div >
<div ></div>
</div>
<div >
<div >
<div >
<div >
<div >
<div >
<h2 dir="auto">
<span dir="auto">
<span style="-webkit-box-orient: vertical; -webkit-line-clamp: 2; display: -webkit-box;">Marketplace listings – 72</span>
</span>
</h2>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
CodePudding user response:
Try using this xpath expression instead:
driver.find_element(By.XPATH,"//span[@dir='auto']/span").get_attribute("innerText")
I tested the expression with your html example and confirmed it works.
OUTPUT
'Marketplace listings – 72'
CodePudding user response:
Considering the effective portion from the given HTML:
<h2 dir="auto">
<span dir="auto">
<span style="-webkit-box-orient: vertical; -webkit-line-clamp: 2; display: -webkit-box;">Marketplace listings – 72</span>
</span>
</h2>
To extract the text Marketplace listings – 72 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, "h2[dir="auto"] span[dir="auto"] > span[style*='webkit-box-orient']"))).text)
Using XPATH and
get_attribute("innerHTML")
:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[@dir="auto"]/span[@dir="auto"]/span[contains(@style, 'webkit-box-orient') and text()]"))).get_attribute("innerHTML"))
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