Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium中的Instagram XPath赢得';不行?(Python)_Python_Selenium_Xpath_Instagram - Fatal编程技术网

Selenium中的Instagram XPath赢得';不行?(Python)

Selenium中的Instagram XPath赢得';不行?(Python),python,selenium,xpath,instagram,Python,Selenium,Xpath,Instagram,我在Selenium中创建Instagram bot基本上是为了喜欢其他Instagram帐户的某些评论,但我不能。单击某个评论评论部分Instagram心脏的xpath()。我真的只需要在Instagram的评论部分中找到正确的xpath,我就完成了 from webdriver_manager.chrome import ChromeDriverManager from time import sleep from selenium.webdriver.support.ui import W

我在Selenium中创建Instagram bot基本上是为了喜欢其他Instagram帐户的某些评论,但我不能。单击某个评论评论部分Instagram心脏的xpath()。我真的只需要在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```

我使用了下面的xpath来查找like按钮,它对我很有用

driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div[1]/article/div[2]/section[1]/'
                                         'span[1]/button').click()
从你的代码中我看到的是

/section[1]/span[1]/button/svg
但它应该是公正的

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

因为你想点击按钮而不是svg,所以我最终使用了
//span/div/button[*[local-name()='svg']/@aria label='Like']
,但我也会尝试使用它。这只是我学到的一个小技巧。。。如果你走一条完整的路,事情很可能不会那么容易打破。。。此外,如果您使用xpath,请尝试转到根,如果它崩溃,请查看错误的位置。。。。另外,使用time.sleep,而不是驱动程序等待,只是为了让事情简单化,然后让它们一直休眠,直到它们可以点击为止(如果你让它等待5秒,你可以看到它的点击,但它会崩溃,这意味着你有xpath的问题…很多时候我看到一个代码,人们只是做了错误的等待部分,并努力使其工作…无论如何,在编码!