David Mustea
David Mustea

Reputation: 67

Selenium error when trying to find xpath for "Not Now" button

I want to do a Instagram bot and I am at the the beggining. Selenium doesn't want to click the "Not Now" Button because of this error:

"selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //button[text() = "Not now" because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//button[text() = "Not now"' is not a valid XPath expression."

Here is the code by now:

from selenium import webdriver
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
numeleUsername = input("username: ")
parolaPassword = input("password: ")

driver = webdriver.Chrome(PATH)



driver.get("https://instagram.com/")
time.sleep(2)

driver.find_element_by_xpath("//button[text()='Accept']").click()
username = driver.find_element_by_xpath('//input[@name="username"]')
password = driver.find_element_by_xpath('//input[@name="password"]') 
username.click()
username.send_keys(numeleUsername)
password.click()
password.send_keys(parolaPassword)
driver.find_element_by_xpath('//button[@type = "submit"]').click()
time.sleep(1)
driver.find_element_by_xpath('//button[text() = "Not now"').click()

Upvotes: 0

Views: 1153

Answers (2)

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//button[text() = "Not Now"]'))).click()

Simply wait and click on the element it's Not Now instead of Not now btw.

Import

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

Upvotes: 2

Forensic_07
Forensic_07

Reputation: 1135

You forgot the closing square bracket.

//button[text() = "Not now"]

Upvotes: 1

Related Questions