简体   繁体   English

Selenium 和 XPath。无法在页面 (Instagram) 中找到元素,即使看起来我有正确的路径。 弹性元素?

[英]Selenium and XPath. Can't find an element in a page (Instagram) even though it looks like I have the correct path. Flex element?

I'm doing a webscraping course on Selenium. The overall objective is to scrape the photos on this page ( https://www.instagram.com/dataminer2060/ ).我正在 Selenium 上进行网络抓取课程。总体目标是抓取此页面 ( https://www.instagram.com/dataminer2060/ ) 上的照片。 The sub-objective I'm stuck on is scraping the number of posts (currently 37).我坚持的子目标是抓取帖子数量(目前为 37)。

第一个检查元素第二个检查元素

My code is below.我的代码如下。 It all works to login to Instagram, get past the pop ups.一切都可以登录 Instagram,避开弹出窗口。 The bit I'm struggling with is def(scroll_down) .我正在努力解决的问题是def(scroll_down) I'm getting an unable to locate element error.我收到无法定位元素错误。

I suspect this is because this a flex element but I don't know how to navigate those yet.我怀疑这是因为这是一个 flex 元素,但我还不知道如何导航这些元素。

Any help would be much appreciated任何帮助将非常感激

from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options as ChromeOptions

#to stop Selenium automatically closing

options = ChromeOptions()
options.add_experimental_option("detach", True)

class App:
    def __init__(self, username='my.username', password='mypassword', target_username='dataminer2060',
                 path='/Users/MyUser/Desktop/instaPhotos'): 
        self.username = username
        self.password = password
        self.target_username = target_username
        self.path = path
        self.service = Service('/Users/MyUser/Documents/Python/chromedriver')
        self.driver = webdriver.Chrome(options=options, service=self.service)
        self.error = False
        self.main_url = 'https://www.instagram.com'
        self.driver.get(self.main_url)
        sleep(3)

        self.log_in()
        sleep(3)
        self.open_target_profile()
        sleep(3)
        self.scroll_down()

#HERE IS WHERE I'M STUCK

    def scroll_down(self):
        no_of_posts = self.driver.find_element(By.XPATH, "//span[@class='g47SY lOXF2']")
        no_of_posts = str(no_of_posts.text)
        print(no_of_posts)
        input('stop for now')

#BELOW WORKS

    def open_target_profile(self):
        search_bar = self.driver.find_element(By.XPATH, "//input[@placeholder='Search']")
        search_bar.send_keys(self.target_username)
        target_profile_url = self.main_url + "/" + self.target_username + "/"
        self.driver.get(target_profile_url)
        sleep(3)

    def log_in(self):
        cookies = self.driver.find_element(By.XPATH, "//button[contains(text(), 'Only Allow Essential Cookies')]")
        cookies.click()
        sleep(1)
        login_details = self.driver.find_element(By.XPATH, "//input[@aria-label='Phone number, username or email address']")
        login_details.send_keys('my.username')
        sleep(2)
        password = self.driver.find_element(By.XPATH, "//input[@aria-label='Password']")
        password.send_keys('mypassword')
        sleep(2)
        login_button = self.driver.find_element(By.XPATH, "//div[text()='Log In']")
        login_button.click()
        sleep(3)
        save_info = self.driver.find_element(By.XPATH, "//button[text()='Save information']")
        save_info.click()
        sleep(2)
        not_now2 = self.driver.find_element(By.XPATH, "//button[text()='Not Now']")
        not_now2.click()

if __name__ == '__main__':
    app = App()

I have tried that XPATH, the one for the div parent and the full XPath. I'm expecting to be able to find the element which contains the text with no.我试过 XPATH,div 父级的那个和完整的 XPath。我希望能够找到包含没有文本的元素。 of posts.职位。

Made an error.犯了一个错误。 <span class="g47SY ">37</span> Somehow I added extra code in the class field in my original code. <span class="g47SY ">37</span>我以某种方式在原始代码的 class 字段中添加了额外的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium 通过 xpath 提出错误的元素,即使路径是正确的 - Selenium bringing up wrong element by xpath even though the path is correct 抓取文本; 我不确定 Google Chrome Inspect 元素是否为我提供了正确的 XPath。 我在哪里可以获得正确的路径? - Scraping text; I'm not sure the Google Chrome Inspect element is giving me the correct XPath. Where can I get the correct path? 通过 Xpath 查找元素。如何在 Xpath 中拆分我不想要的元素 - Find element by Xpath. How to split the element I don't want inside the Xpath selenium:即使完整的 xpath 也找不到元素 - selenium: Can not find element even with full xpath 在 xpath 中找不到元素(Instagram、selenium、python) - Cant find element in xpath (Instagram, selenium, python) Selenium find_element_by_xpath 不适用于 instagram - Selenium find_element_by_xpath not working for instagram 找不到元素-xPath正确 - Can't find element - xPath correct 无论我如何尝试都无法在Selenium中通过Xpath找到元素 - Can't find element by Xpath in Selenium no matter what I try Selenium - 无法通过 xpath 找到元素 - Selenium - can't find element by xpath 即使在 Selenium Python 中找到了它的兄弟元素,也找不到元素? - Can't find an element even though its brother element is found in Selenium Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM