【问题标题】:How to Fix: Unable to locate element: method- XPath如何修复:无法定位元素:method-XPath
【发布时间】:2021-11-18 12:37:35
【问题描述】:

This is the website我正在尝试自动化一些点击:

我曾尝试使用XpathFullXpath 单击按钮,但还是没有成功。

这是简单的代码:

w = webdriver.Chrome(executable_path='chromedriver.exe',
                     chrome_options=options)

w.get("https://quillbot.com/")
time.sleep(5)
pasteXpath = "//button[contains(@class,'outlinedPrimary') and .//span[contains(text(),'Paste Text')]]"
element = w.find_element_by_xpath(pasteXpath).click()

但它在控制台中显示此消息失败:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="inOutContainer"]/div[2]/div[2]/div/div[1]/div/div/div[1]/div/div/div[2]/div/div/button/span[1]/div"}

请告诉我如何使用 selenium 自动执行此点击。

【问题讨论】:

  • 您的 xpath 是否在相关页面的浏览器中工作?
  • 在尝试获取元素之前尝试睡眠几秒钟
  • 只是为了进行完整性检查,请尝试对@id='inOutContainer' 使用外部引号和内部简单引号。或者将整个 XPath 放在三引号内 """ 并保留内部双引号
  • 确保元素已经加载到页面中也很重要。您可以使用selenium.webdriver.support.wait.WebDriverWaitselenium.webdriver.support.expected_conditions 非常简洁易读地做到这一点
  • 另外,对我来说,访问您提供的链接的页面看起来不像图像中的那个。首先,我无法访问“扩展”选项,它已被禁用;其次,一个Try Sample Text 按钮显示在Paste Text 的位置,虽然奇怪的是后者在重新加载页面时出现了一会儿,就在前者旁边

标签: python selenium xpath


【解决方案1】:

我建议使用ByWebDriverWaitexpected_conditions 代替.find_element_by_xpath

单击粘贴按钮后,您将收到权限提示。请参阅下文以克服它。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.chrome.service import Service
import time
import pyautogui


service = Service('C:\\Path_To_Your\\chromedriver.exe')
driver = webdriver.Chrome(service=service)
driver.get('https://quillbot.com/')

paste_button = WebDriverWait(driver, 3).until(EC.visibility_of_element_located(
            (By.XPATH, "//span[text()='Paste Text']")))

paste_button.click()
time.sleep(2)

pyautogui.press('tab')
pyautogui.press('tab')
pyautogui.press('enter')

【讨论】:

  • 不错的解决方案。避免使用pyautogui 的另一个选项是使用selenium.webdriver.common.keys.Keys,它与ActionChains 一起允许发送、按下或释放诸如ActionChains(driver).send_keys(Keys.TAB) 之类的键
  • @MatBBastos 我已经尝试使用selenium.webdriver.common.keys.KeysActionChainsActionChains(driver).send_keys(Keys.TAB),但无法让它在这个特定站点上工作。
  • 哦,这出乎意料。好吧,那很好,你找到了答案的另一个选择,太好了!
  • raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException ,不工作的伙伴。我尝试将时间增加到 60 秒,仍然出现此错误
  • @Sainita 你用的是什么浏览器?如果我使用 Firefox,我也不会显示该按钮,我必须使用 Chrome。
【解决方案2】:

这将起作用:

pasteXpath = "//button[contains(@class,'outlinedPrimary') and .//span[contains(text(),'Paste Text')]]"
element = w.find_element_by_xpath(pasteXpath).click()

不要忘记在它之前添加一些等待/延迟以确保页面完全加载。

【讨论】:

  • selenium.common.exceptions.NoSuchElementException: 消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//button[contains(@class, 'outlinedPrimary') 和 .//span[contains(text(),'Paste Text')]]"}
  • 也许您在此代码行之前缺少一些延迟/等待以使页面加载?
  • 加了一个 time.sleep(10) ,还不够吗?
  • 通常已经足够了。仍然有同样的异常?
  • 这就是我说的先生,为什么你的代码不起作用?
【解决方案3】:

尝试使用 CSS 选择器:

element = w.find_element_by_css_selector('div[class*="MuiGrid-root"] > div[class="jss473"]').click()

你可以找到所有关于css选择器here的文档

【讨论】:

  • selenium.common.exceptions.NoSuchElementException: 消息:没有这样的元素:无法找到元素:{"method":"css selector","selector":"div[class*="MuiGrid-根"] > div[class="jss473"]"}
猜你喜欢
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 2021-08-01
  • 2017-12-25
  • 1970-01-01
  • 2021-02-11
  • 2016-07-01
  • 1970-01-01
相关资源
最近更新 更多