ayush bajpai
ayush bajpai

Reputation: 11

Unable to locate element : xpath

I have made a youtube automation bot. I am getting error : unable to locate element (for the Xpath of subscribe button)

here is my code

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)

how can i resolve this issue. I am not able to understand where things are going wrong. Or i should try find elements by id or other ways instead of Xpath. Or is there any problem with any software. Please help me out

Upvotes: 0

Views: 129

Answers (2)

manul wickramanayaka
manul wickramanayaka

Reputation: 27

You can refer how to write XPath in different ways using functions like text(), starts-with(), contains(). so you can locate them by visible texts also. Refer this articlehere

Upvotes: 0

Methmal Godage
Methmal Godage

Reputation: 1106

Always use relative XPath in your test. Using the absolute XPath will cause regular test failures.

Refer to this tutorial about writing the relative XPaths. https://www.guru99.com/xpath-selenium.html

This extension will help you to write the relative XPaths. https://chrome.google.com/webstore/detail/chropath/ljngjbnaijcbncmcnjfhigebomdlkcjo

Upvotes: 0

Related Questions