Selenium span text python

Extract text from span element using selenium in python

I’m trying to extract the contact number (XXXXX-XXXXXX) from the following HTML code using Selenium webdriver’s find_element_by_xpath method.

  
Contact Info
Mobile
XXXXX-XXXXXX
print(browser.find_element_by_xpath('//div[@id="contact-info"]/div[1]/div[2]/div[1]/div[2]/span[1]/span[1]').text) 

But I’m thrown with an exception saying the element can’t be located. What am I possibly doing wrong here? How can I fix this?

3 Answers 3

To get the Mobile-Number you can take the reference of the text Mobile and find the next sibling.

Induce WebDriverWait () and wait for visibility_of_element_located () and following xpath.

print(WebDriverWait(browser,10).until(EC.visibility_of_element_located((By.XPATH,"//td[.//div[contains(.,'Mobile')]]/following-sibling::td[1]/div"))).text) 

You need to import following libraries.

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

@AbdullahShahriar : The xpath is fine.I have tested before posted as an answer.Could you just check if there any iframe above your table element?

print(browser.find_element_by_xpath('//span[@dir="ltr"]').text) 
contactNumber = WebDriverWait(browser, 30).until( EC.element_to_be_clickable((By.XPATH, "//div[@class='random_class_name']//span[1]//span"))) print contactNumber.text 
contactNumber = WebDriverWait(browser, 30).until( EC.element_to_be_clickable((By.XPATH, "//span[contains(text(), 'XXXXX-XXXXXX')]"))) print contactNumber.text 

Note : add below imports to your solution :

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

Upddated Section: Check your table is in the iframe if so then use below code to swiutch to iframe ;

iframe=driver.find_element_by_tag_name("iframe") driver.switch_to.frame(iframe) 

Источник

Selenium — cant get text from span element

I’m very confused by getting text using Selenium. There are span tags with some text inside them. When I search for them using driver.find_element_by_. , everything works fine. But the problem is that the text can’t be got from it. The span tag is found because I can’t use .get_attribute(‘outerHTML’) command and I can see this:

But if I change .get_attribute(‘outerHTML’) to .text it returns empty text which is not correct as you can see above. Here is the example (outputs are pieces of dictionary):

display_site = element.find_element_by_css_selector('span.branding').get_attribute('outerHTML') 
display_site = element.find_element_by_css_selector('span.branding').text 

As you can clearly see, there is a text but it does not finds it. What could be wrong? EDIT: I’ve found kind of workaround. I’ve just changed the .text to .get_attribute(‘innerText’) But I’m still curious why it works this way?

Since I am new to Selenium I do not want to post an answer out of the blue. Could you provide a proper URL to test my attempt? I think, find_element_by_class_name(‘branding’).text could do the job.

I think this can’t help because the tag is properly found, thats because I’ve attached .get_attribute(‘outerHTML’). But of course — altpress.com/aptv/video/…

driver.get(‘altpress.com/aptv/video/…) print driver.find_element_by_css_selector(‘span.branding’).get_attribute(‘outerHTML’) returns

Getting the text StartFluff out of the first tag is not a problem: for e in driver.find_elements_by_class_name(‘branding’): print(e.text)

Does display_site = element.find_element_by_css_selector(‘span.branding’).get_attribute(‘innerHTML’) work?

1 Answer 1

The problem is that there are a LOT of tags that are fetched using span.branding . When I just queried that page using find_elements (plural), it returned 20 tags. Each tag seems to be doubled. I’m not sure why but my guess is that one set is hidden while the other is visible. From what I can tell, the first of the pair is hidden. That’s probably why you aren’t able to pull text from it. Selenium’s design is to not interact with elements that a user can’t interact with. That’s likely why you can get the element but when you try to pull text, it doesn’t work. Your best bet is to pull the entire set with find_elements and then just loop through the set getting the text. You will loop through like 20 and only get text from 10 but it looks like you’ll still get the entire set anyway. It’s weird but it should work.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.27.43548

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How to click on span element with python selenium

Im trying to click on this for whole day with python selenium with no luck, tried several selectors, xpath..nothing seems to be work for me. This is the element I try to click on:

driver.find_element_by_link_text("No") 

2 Answers 2

Search by link text can help you only if your span is a child of anchor tag, e.g. No . As you’re trying to click it, I believe it’s really inside an anchor, but if not I’d suggest you to use XPath with predicate that returns True only if exact text content matched:

Note that //span[contains(text(), «No»)] is quite unreliable solution as it will return span elements with text

If you get NoSuchElementException you might need to wait for element to appear in DOM :

from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait as wait wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='No']"))).click() 

I was doing something in my project too for Spotify. This is the function I wrote to select genders which are in span html tags.

Libraries Required

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

Defining Variables

gender_male = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Male']"))) gender_female = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Female']"))) non_binary = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Non-binary']"))) other = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Other']"))) pnts = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Prefer not to say']"))) 

Using Random

gender_guess = random.randint(1, 5) if gender_guess == 1: gender_male.click() elif gender_guess == 2: gender_female.click() elif gender_guess == 3: non_binary.click() elif gender_guess == 4: other.click() elif gender_guess == 5: pnts.click() 

Full Code

from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC gender_male = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Male']"))) gender_female = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Female']"))) non_binary = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Non-binary']"))) other = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Other']"))) pnts = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Prefer not to say']"))) gender_guess = random.randint(1, 5) if gender_guess == 1: gender_male.click() elif gender_guess == 2: gender_female.click() elif gender_guess == 3: non_binary.click() elif gender_guess == 4: other.click() elif gender_guess == 5: pnts.click() 

Источник

How to get text from span using selenium

I’m trying to get text from span (the quality in the picture), but I haven’t find the reason why it’s not getting it. thanks for all the helpers, I’m using python here. my code:

 elem=driver.findElement(By.XPATH("//span[@class='ytp-menu-label-secondary']")); 

The HTML

from the html :

What code are you using to get the element’s text? The code you posted just finds the element itself.

elem=driver.findElement(By.XPATH(«//span[@class=’ytp-menu-label-secondary’]»)).text I want to get the element and then extract the text there.

So when you try that code, what happens? Do you get different text than you expected, or no text at all, or an error?

Please post the HTML as text in the question instead of linking an image. At some point the image no longer be available and this question will be less useful to others.

2 Answers 2

element = driver.find_element_by_xpath("//span[@class='ytp-menu-label-secondary']") 

Or by class name if you like

element = driver.find_element_by_class_name('ytp-menu-label-secondary') 

excerpt from this link: As defined in WebDriver spec, Selenium WebDriver will only interact with visible elements, therefore the text of an invisible element will always be returned as an empty string.

However, in some cases, one may find it useful to get the hidden text, which can be retrieved from element’s textContent, innerText or innerHTML attribute, by calling element.attribute(‘attributeName’) or injecting JavaScript like return arguments[0].attributeName.

innerHTML will return the inner HTML of this element, which contains all HTML tags inside. For example, innerHTML for Hello

instead of Hello World!. textContent and innerText will only retrieve all text content of its descendants without any HTML tags. textContent is a W3C-compliant textContent property[1], but sadly is not supported by IE[2]. innerText is not part of the W3C DOM specification and not supported by Firefox. Here is a brief demonstration on how to get text from hidden elements using Selenium WebDriver .NET, Ruby and Python bindings.

from selenium import webdriver

Demo page for how to get text from hidden elements using Selenium WebDriver.

Demo div with a hidden paragraph inside.

driver = webdriver.PhantomJS() driver.get(DEMO_PAGE) demo_div = driver.find_element_by_id("demo-div") print demo_div.get_attribute('innerHTML') print driver.execute_script("return arguments[0].innerHTML", demo_div) print demo_div.get_attribute('textContent') print driver.execute_script("return arguments[0].textContent", demo_div) driver.quit 

Источник

Читайте также:  Fread and fwrite in php
Оцените статью