简体   繁体   English

Selenium 中的 Instagram XPath 不起作用? (Python)

[英]Instagram XPath in Selenium Won't Work? (Python)

Im creating an Instagram bot in Selenium basically to like certain comments with other Instagram accounts, but Im unable to.click() on the xpath of Instagram's heart in the comment section of a certain comment.我在 Selenium 中创建了一个 Instagram 机器人,基本上是为了喜欢其他 Instagram 帐户的某些评论,但我无法在某个评论的评论部分中 Instagram 的 xpath 上的 xpath。 I really just need the correct xpath of the heart in Instagram's comment section, and I'll be done with this.我真的只需要在 Instagram 的评论部分中正确的 xpath 心脏,我会完成这个。

from webdriver_manager.chrome import ChromeDriverManager
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import urllib

Link = 'https://www.instagram.com/p/CCZDF7YH5Yb/c/17958711199328488/'
driver = webdriver.Chrome(ChromeDriverManager().install())


driver.get('https://www.instagram.com/')
driver.implicitly_wait(10)

driver.find_element_by_name('username').send_keys('username') #Changed for the purpose of making this thread
driver.find_element_by_name('password').send_keys('password')
Login = "//button[@type='submit']"
driver.find_element_by_xpath(Login).submit()
sleep(1)
#Logs into Instagram
print ('Logged In')

NotNow = "//button[contains(text(),'Not Now')]"
driver.find_element_by_xpath(NotNow).click()
#Clicks Pop Up
print ('Close Pop Up')

#ISSUE is Below

#CommentHeart = '//*[@id="react-root"]/section/main/div/div[1]/article/div[3]/section[1]/span[1]/button/svg' #THIS XPATH doesn't work?
driver.get(Link)
wait = WebDriverWait(driver, 20)
element = wait.until(EC.element_to_be_clickable((By.XPATH, '/main/div/div[1]/article/div[3]/div[1]/ul/div[2]/ul/div/li/div/span/div/button/svg'))) #THIS XPATH doesn't work
element.click()
#driver.find_element_by_xpath(CommentHeart).click()
print ('Likes Comment')```

This is the xpath I've been using, that doesn't work.
```//*[@id="react-root"]/section/main/div/div[1]/article/div[3]/div[1]/ul/div[2]/ul/div/li/div/span/div/button/svg```

I used the xpath below to find the like button and it works for me我使用下面的 xpath 找到了类似的按钮,它对我有用

driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div[1]/article/div[2]/section[1]/'
                                         'span[1]/button').click()

from what I can see in your code is that you have从我在你的代码中看到的是你有

/section[1]/span[1]/button/svg

but it should be just但应该只是

/section[1]/span[1]/button

since you want to click on the button and not svg因为你想点击按钮而不是 svg

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM