I am working on 30 Days of Python - Day 16 - Scrape & Automate behind Password Protected Apps with Selenium & Python.
However, I found that using XPATH to find the button 'Follow' in Instagram is not working.
def click_to_follow(browser):
my_follow_btn_xpath = '//*[contains(text()='follow')]'
follow_btn_els = browser.find_elements(By.XPATH, my_follow_btn_xpath)
for btn in follow_btn_els:
try:
btn.click()
except:
pass
new_user = 'ted'
new_user_url = f'http://www.instagram.com/{new_user}/'
browser.get(new_user_url)
click_to_follow(browser)
The above is the code, but it shows that I had an invalid syntax.
my_follow_btn_xpath = '//*[contains(text()='follow')]'
^^^^^^
SyntaxError: invalid syntax
Could any one help with this? I have search for similar posts but I still can't work it out.
CodePudding user response:
You can't use single quotes inside your xpath.
Change this:
my_follow_btn_xpath = '//*[contains(text()='follow')]'
to this:
my_follow_btn_xpath = '//*[contains(text()="follow")]'
CodePudding user response:
This would work:
Note: You took Follow
as follow
. The text is Follow
, not follow
DOM
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//div[text()='Follow']"))).click()
This would be the specific element that you want:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//h1//parent::div//div[text()='Follow']"))).click()