How to find button with Selenium by its text inside (Python)?
Question:
Answers:
Try this XPath:
"//button[@class='three-state-item btn btn-default'][.='Outliers']"
.
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:
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()
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("https://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("https://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()
Try this XPath:
"//button[@class='three-state-item btn btn-default'][.='Outliers']"
.
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:
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()
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("https://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("https://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()