【问题标题】:Selenium error when trying to find xpath for "Not Now" button尝试为“Not Now”按钮查找 xpath 时出现 Selenium 错误
【发布时间】:2021-06-20 12:40:00
【问题描述】:

我想做一个 Instagram 机器人,我还处于起步阶段。 由于这个错误,Selenium 不想点击“Not Now”按钮:

"selenium.common.exceptions.InvalidSelectorException: 消息:无效选择器:由于以下错误,无法使用 xpath 表达式 //button[text() = "Not now" 定位元素: SyntaxError: 无法对 'Document' 执行 'evaluate': 字符串 '//button[text() = "Not now"' 不是有效的 XPath 表达式。"

现在是代码:

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()

【问题讨论】:

    标签: python selenium xpath


    【解决方案1】:
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//button[text() = "Not Now"]'))).click()
    

    只需等待并点击它的元素 Not Now 而不是 Not now 顺便说一句。

    导入

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

    【讨论】:

      【解决方案2】:

      您忘记了右方括号。

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

      【讨论】:

      • 我不敢相信我为此在 StackOverflow 上提出了问题
      • 老兄,我检查了 3 次
      • 不,不是这个
      • 将 HTML 代码添加到您的 OP 中,以便我们更好地了解发生了什么
      • 尝试交换你的报价。两端用双引号括起来,单引号中的“现在不是”
      猜你喜欢
      • 2023-02-09
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 2023-01-30
      • 2017-10-19
      相关资源
      最近更新 更多