[英]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.