View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
In this article, let’s discuss how to find a button by text using selenium. See the below example to get an idea about the meaning of the finding button by text.
Example:
URL: //html.com/tags/button/
We need to find the “CLICK ME!” button using the text “Click me!”.
Module Needed:
Selenium: The selenium package is used to automate web browser interaction from Python. It is an open-source tool primarily used for testing. Run the following command in the terminal to install this library:
pip install selenium
Setup Web Drivers:
Web Driver is a package to interact with a Web Browser. You can install any Web Driver according to your browser choice. Install any one of them using the given links-
Here, we are going to use ChromeDriver.
Find xpath of the button:
- Method 1: Using Inspect Element
Right Click on the element you are trying to find the xpath. Select the “Inspect” option.
- Right click on the highlighted area on the console. Go to Copy xpath
- Method 2: Using Chrome Extension to find xpath easily:
We can easily find xpath of an element using a Chrome extension like SelectorGadget.
Approach:
- Import Selenium and time library
- Set the Web Driver path with the location where you have downloaded the WebDriver
Example- “C:\\chromedriver.exe” - Call driver.get[] function to navigate to a particular URL.
- Call time.sleep[] function to wait for the driver to completely load the webpage.
- Use driver.find_element_by_xpath[] method to find the button using xpath.
- Finding button by text-
[i] Using normalize-space[] method:
driver.find_element_by_xpath[‘//button[normalize-space[]=”Click me!”]’]
[ii] Using text[] method:
driver.find_element_by_xpath[‘//button’]
Note: It is recommended to use normalize-space[] method because it trim the left and right side spaces. It is possible that there can be spaces present at the start or at the end of the target text. - Lastly close the driver using driver.close[] function.
Implementation:
Python3
from
selenium
import
webdriver
import
time
driver
=
webdriver.Chrome[executable_path
=
r
"C:\\chromedriver.exe"
]
time.sleep[
5
]
driver.find_element_by_xpath[
'//button[normalize-space[]="Click me!"]'
].click[]
time.sleep[
5
]
driver.close[]
Output:
//media.geeksforgeeks.org/wp-content/uploads/20210227233751/Find-Button-By-text.mp4
I have the following three buttons that I can't figure out how to grab the text that is inside of them [e.g Outliers]. I tried browser.find_element_by_link_text["Outliers"].click[]
, but got "Unable to locate element" error. How can I do it?
asked Apr 18, 2018 at 18:03
See: find_element_by_* commands are deprecated in selenium
In newer versions of selenium try:
from selenium.webdriver.common.by import By
browser.find_element[By.XPATH, '//button[text[]="Outliers"]']
older versions of selenium:
browser.find_element_by_xpath['//button[text[]="Outliers"]']
To update ALL of the older versions I found a nifty regex here, and then just fixup the import:
- //stackoverflow.com/a/70586710/2026508
answered Apr 18, 2018 at 18:09
jmunschjmunsch
20.6k10 gold badges85 silver badges107 bronze badges
1
There are two ways :
- By using text[] method:
browser.find_element[By.XPATH,'//button[text[]="Outliers"]']
- By using normalize-space[] method:
browser.find_element[By.XPATH, '//button[normalize-space[]="Outliers"]']
Note : It is always better to use normalize-space[] method as it will work even if there are spaces present at the start of your text or at the end of text, because normalize-space[] method trim the left and right side spaces
For More information on Normalize-space[]
answered Apr 18, 2018 at 18:43
Pritam MaskePritam Maske
2,5422 gold badges19 silver badges28 bronze badges
1
Try this XPath:
"//button[@class='three-state-item btn btn-default'][.='Outliers']"
.
answered Apr 18, 2018 at 18:07
GrasshopperGrasshopper
8,6812 gold badges15 silver badges31 bronze badges
3
This is the solution that worked for me:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
CHROME_DRIVER_PATH = Your Driver Path
USERNAME = YOUR USERNAME
PASSWORD = YOUR PASSWORD
SIMILAR_ACCOUNT = "idogsplanet"
class InstaFollower:
def __init__[self, driver_path]:
self.driver = webdriver.Chrome[executable_path=driver_path]
def login[self]:
self.driver.get["//www.instagram.com/accounts/login/"]
time.sleep[3]
username = self.driver.find_element_by_name["username"]
username.send_keys[USERNAME]
password = self.driver.find_element_by_name["password"]
password.send_keys[PASSWORD]
time.sleep[2]
login = self.driver.find_element_by_xpath['//*[@id="loginForm"]/div/div[3]/button/div']
login.click[]
def find_followers[self]:
time.sleep[5]
self.driver.get["//www.instagram.com/" + SIMILAR_ACCOUNT + "/followers"]
followers = self.driver.find_element_by_xpath['//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a']
followers.click[]
time.sleep[1]
def follow[self]:
all_buttons = self.driver.find_elements_by_xpath['//button[normalize-space[]="Follow"]']
modal = self.driver.find_element_by_xpath['/html/body/div[5]/div/div/div[2]']
for button in all_buttons:
if button.text != "Follow":
pass
else:
button.click[]
time.sleep[2]
time.sleep[10]
self.driver.execute_script["arguments[0].scrollTop = arguments[0].scrollHeight", modal]
self.follow[]
bot = InstaFollower[CHROME_DRIVER_PATH]
bot.login[]
bot.find_followers[]
bot.follow[]
answered Jul 2, 2021 at 15:03