'Extracting text from button with selenium
I'm trying to use selenium to extract the text on a button but python is only returning None
I got the button using
button = browser.find_element_by_xpath("/html/body/div[3]/main/div[3]/div[3]/div/div/div/div/div/button")
text = button.get_attribute('text')
(Button works perfectly fine when using .click() )
This is the button code:
<button class=" btn btn-primary stepbuttonnew" onclick="if (!window.__cfRLUnblockHandlers) return false; ok.performClick();gtag('config', 'UA-113527404-1', {'page_path': '/smth'});">
Text i want
</button>
It should return "Text I want"
Any help is appreciated :)
Solution 1:[1]
Can you post the page? I would suggest to try :
text = driver.find_element_by_xpath('XPATH').text
print(text)
Maybe test find_element_by_class_name if xpath won't work.
Solution 2:[2]
The text property of your button element will return the text.
button = browser.find_element_by_xpath("/html/body/div[3]/main/div[3]/div[3]/div/div/div/div/div/button")
buttonText = button.text
Solution 3:[3]
If the usual selenium does not work then I believe that JavaScript executor will be able to help you to retrieve the desired value. If you do not have prior knowledge on it then I recommend you to check that out.
Solution 4:[4]
`using innerText atttribute? many times plain text reads null in Angular/React ui applicatios, you should be able to get it using innerText attribute i.e , text = button.get_attribute('innertext')
Solution 5:[5]
For scraping text from button in Python, you can coordinate ProxyCrawl API(https://pypi.org/project/proxycrawl/) that is sufficiently adaptable to blend with your current Python codebase. APIs are in effect nowadays and is utilized by information engineers these days as they give more proficient, safe, productive information scraping and are totally versatile as indicated by the dynamism of various pages. Also, we can fetch the button text by CSS Selector, XPath and Class name. Other than this, you can likewise utilize the accompanying code to scrape information utilizing Python Selenium.
Source Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
driver = webdriver.Chrome()
wait = WebDriverWait(driver,30)
driver.get("https://www.decoin.io/en/")
driver.maximize_window()
text = wait.until(EC.visibility_of_element_located((By.XPATH,'/html/body/section[1]/div/div[2]/a[1]/button'))).text
print(text)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | kk_Chiron |
| Solution 2 | AutomatedOrder |
| Solution 3 | UnknownBeast |
| Solution 4 | SeleniumBeginner |
| Solution 5 | Bilal Ahmed |
