【问题标题】:Unable to locate element : xpath无法定位元素:xpath
【发布时间】:2021-08-01 06:11:06
【问题描述】:

我制作了一个 youtube 自动化机器人。我收到错误:无法定位元素(对于订阅按钮的 Xpath)

这是我的代码

from selenium import webdriver
from selenium import common
from selenium.webdriver.common import keys
from webdriver_manager.firefox import GeckoDriverManager
import time


class actions:

    def __init__(self, email, password):
        self.email = email
        self.password = password
        profile = webdriver.FirefoxProfile()
        profile.set_preference("dom.webdriver.enabled", False)
        profile.set_preference('useAutomationExtension', False)
        profile.update_preferences()

        driver = webdriver.Firefox(
            executable_path=GeckoDriverManager().install(), firefox_profile=profile)
        self.bot = driver
        # self.bot.maximize_window()
        self.bot.set_window_size(400, 700)
        self.is_logged_in = False

    def login(self):
        bot = self.bot
        bot.get("https://accounts.google.com/signin/v2/identifier?service=youtube&uilel=3&passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26app%3Ddesktop%26hl%3Den%26next%3Dhttps%253A%252F%252Fwww.youtube.com%252F&hl=en&ec=65620&flowName=GlifWebSignIn&flowEntry=ServiceLogin")
        time.sleep(5)

        try:
            email = bot.find_element_by_name('identifier')
        except common.exceptions.NoSuchElementException:
            time.sleep(5)
            email = bot.find_element_by_name('identifier')

        email.clear()
        email.send_keys(self.email + keys.Keys.RETURN)
        time.sleep(5)

        try:
            password = bot.find_element_by_name('password')
        except common.exceptions.NoSuchElementException:
            time.sleep(5)
            password = bot.find_element_by_name('password')

        password.clear()
        password.send_keys(self.password + keys.Keys.RETURN)
        time.sleep(5)
        self.is_logged_in = True

    def kill(self):
        bot = self.bot
        bot.quit()

    def subscribe(self, url):
        if not self.is_logged_in:
            return

        bot = self.bot
        bot.get(url)
        time.sleep(4)

        try:
            value = bot.find_element_by_xpath(
                '/html/body/ytd-app/div/ytd-page-manager/ytd-watch-flexy/div[5]/div[1]/div/div[7]/div[2]/ytd-video-secondary-info-renderer/div/div/div/ytd-subscribe-button-renderer/tp-yt-paper-button').get_attribute('aria-label')
            value = value.split()
        except:
            bot.execute_script(
                'window.scrollTo(0,document.body.scrollHeight/3.5)')
            time.sleep(3)
            value = bot.find_element_by_xpath(
                '/html/body/ytd-app/div/ytd-page-manager/ytd-watch-flexy/div[5]/div[1]/div/div[7]/div[2]/ytd-video-secondary-info-renderer/div/div/div/ytd-subscribe-button-renderer/tp-yt-paper-button').get_attribute('aria-label')
            value = value.split(':')

        if value[0] == "Subscribe":
            try:
                bot.find_element_by_xpath(
                    '/html/body/ytd-app/div/ytd-page-manager/ytd-watch-flexy/div[5]/div[1]/div/div[7]/div[2]/ytd-video-secondary-info-renderer/div/div/div/ytd-subscribe-button-renderer/tp-yt-paper-button').click()
                time.sleep(3)
            except:
                bot.execute_script(
                    'window.scrollTo(0,document.body.scrollHeight/3.5)')
                time.sleep(3)
                bot.find_element_by_xpath(
                    '/html/body/ytd-app/div/ytd-page-manager/ytd-watch-flexy/div[5]/div[1]/div/div[7]/div[2]/ytd-video-secondary-info-renderer/div/div/div/ytd-subscribe-button-renderer/tp-yt-paper-button').click()
        time.sleep(3)

我该如何解决这个问题。我无法理解哪里出了问题。或者我应该尝试通过 id 或其他方式而不是 Xpath 查找元素。 或者有什么软件有问题。 请帮帮我

【问题讨论】:

  • 你的 xpath 被塑造成易碎的。假设这不是问题,时间可能是。您应该对元素使用等待。
  • 您收到错误的代码行是什么?什么xpath 不起作用?是的,你的xpaths 很糟糕。你不应该使用绝对的xpaths。
  • 你能多指导我一点吗?等待元素部分,任何我可以参考和指导我的来源@DMart
  • 我在第 65 行遇到错误,你能告诉我创建或找到完美相对 xpath 的方法吗? @先知
  • 没有“方法”,你应该学习如何构建xpath 表达式。有很多免费的简短教程

标签: selenium firefox xpath webdriver geckodriver


【解决方案1】:

在您的测试中始终使用相对 XPath。使用绝对 XPath 会导致常规测试失败。

有关编写相对 XPath 的信息,请参阅本教程。 https://www.guru99.com/xpath-selenium.html

此扩展将帮助您编写相关的 XPath。 https://chrome.google.com/webstore/detail/chropath/ljngjbnaijcbncmcnjfhigebomdlkcjo

【讨论】:

  • 谢谢先生的帮助。您能否建议我为 Firefox 编写任何扩展来编写相关 Xpath 或者我可以使用您提到的与 Firefox 相关的 xpath 相同的扩展?
  • 更改为相对 xpath 后,我遇到了同样的错误,现在可能是问题所在。出现错误.... 无法定位元素。请告诉我。
【解决方案2】:

您可以参考如何使用 text()、starts-with()、contains() 等函数以不同方式编写 XPath。因此您也可以通过可见文本找到它们。 参考这篇文章here

【讨论】:

    猜你喜欢
    • 2021-02-11
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 2018-03-15
    • 2019-10-13
    • 1970-01-01
    相关资源
    最近更新 更多