我试图用 selenium 做一个instagrambot。我试图点击下拉菜单中的文章或故事,当检查(开发者工具)打开时,当我第一次点击创建[+]按钮时,它们不工作。在终端中,它看起来只是暂停,不移动到下一个元素,它没有给予错误。
但是当我自己点击create [+]按钮,等待“post”xpath点击post时,它就工作了,打开了filedialog。
<div aria-hidden="false" class="_ad8j _aa5x _aa5-"><div class="_aa61"><div class="_abm4" aria-disabled="false" role="button" tabindex="0" style="cursor: pointer;"><div aria-labelledby="f164fa7b8b81fb f25b63fbbbaab04 fa96d5b7069bd8 f8578df4b0a8" class="_ab8w _ab94 _ab97 _ab9f _ab9k _ab9p _ab9- _aba8 _abcm"><div class="_ab8w _ab94 _ab99 _ab9h _ab9m _ab9o _abcm"><div class="_ab8w _ab94 _ab99 _ab9f _ab9m _ab9p _abcm" id="f25b63fbbbaab04"><div class="_aacl _aaco _aacu _aacx _aada"><div class="_ab8w _ab94 _ab99 _ab9h _ab9m _ab9p _abbi _abcm" style="height: 28px;"><div class="_aacl _aaco _aacu _aacx _aada">Post</div></div></div></div></div><div class="_ab8w _ab94 _ab99 _ab9f _ab9m _ab9p _abb1 _abcm"><svg aria-label="Post" class="_ab6-" color="#fafafa" fill="#fafafa" height="16" role="img" viewBox="0 0 24 24" width="16"><rect fill="none" height="18" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" width="18" x="3" y="3"></rect><line fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="9.015" x2="9.015" y1="3" y2="21"></line><line fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="14.985" x2="14.985" y1="3" y2="21"></line><line fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="21" x2="3" y1="9.015" y2="9.015"></line><line fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="21" x2="3" y1="14.985" y2="14.985"></line></svg></div></div></div><div class="_abm4" aria-disabled="false" role="button" tabindex="0" style="cursor: pointer;"><div aria-labelledby="f4f45cbdcdfb08 f38d9baa8e9f58 f151c2901bc3fe4 ff0e85333e79a8" class="_ab8w _ab94 _ab97 _ab9f _ab9k _ab9p _ab9- _aba8 _abcm"><div class="_ab8w _ab94 _ab99 _ab9h _ab9m _ab9o _abcm"><div class="_ab8w _ab94 _ab99 _ab9f _ab9m _ab9p _abcm" id="f38d9baa8e9f58"><div class="_aacl _aaco _aacu _aacx _aada"><div class="_ab8w _ab94 _ab99 _ab9h _ab9m _ab9p _abbi _abcm" style="height: 28px;"><div class="_aacl _aaco _aacu _aacx _aada">Story</div></div></div></div></div><div class="_ab8w _ab94 _ab99 _ab9f _ab9m _ab9p _abb1 _abcm"><svg aria-label="Story" class="_ab6-" color="#fafafa" fill="#fafafa" height="16" role="img" viewBox="0 0 24 24" width="16"><path d="M3.914 5.32q.338-.406.714-.778m-3.122 7.855a9.903 9.903 0 0 1-.007-.386 10.49 10.49 0 0 1 .517-3.265m3.242 11.337a10.568 10.568 0 0 1-2.89-3.864m14.482 5.109a10.535 10.535 0 0 1-8.162.649M12.001 1.51a10.505 10.505 0 0 1 7.926 17.392" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"></path><line fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="12" x2="12" y1="8.006" y2="16.006"></line><line fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="16" x2="8" y1="12.006" y2="12.006"></line></svg></div></div></div></div><div class="_aa62"></div></div>
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import time
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument("--auto-open-devtools-for-tabs")
chrome_options.add_argument("user-data-dir=/Users/smitty/Library/Application Support/Google/Chrome")
chrome_options.add_argument("--profile-directory=Profile 3")
driver = webdriver.Chrome(executable_path="/Users/smitty/Downloads/chromedriver_mac64/chromedriver", options=chrome_options)
driver.get("https://www.instagram.com/")
driver.refresh()
time.sleep(10)
create = driver.find_element("xpath", '/html/body/div[2]/div/div/div/div[1]/div/div/div/div[1]/div[1]/div[2]/section/div[1]/nav/div/header/div/div/div[1]/div[1]/div[2]/a/div')
create.click()
create.click()
#### It works but not when I have the create enabled
time.sleep(5)
Post = driver.find_element("xpath", '/html/body/div[2]/div/div/div/div[1]/div/div/div/div[1]/div[1]/div[2]/section/div[1]/nav/div/header/div/div/div[1]/div[1]/div[1]/div/div/div[1]/div[1]/div')
Post.click()
# time.sleep(10)
# Story = driver.find_element("xpath", '/html/body/div[2]/div/div/div/div[1]/div/div/div/div[1]/div[1]/div[2]/section/div[1]/nav/div/header/div/div/div[1]/div[1]/div[1]/div/div/div[1]/div[2]/div')
# Story.click()
# time.sleep(10)
# driver.quit()
1条答案
按热度按时间siv3szwd1#
通常的
.click()
不适用于[+]按钮:脚本挂起这个命令,不知道为什么,可能是驱动程序认为页面还没有加载.然后我尝试使用javascript,它的工作:点击后会打开一个下拉菜单。我尝试了几种方法点击“发布”按钮,最后找到了一种有效的方法:
点击后会弹出一个窗口,让你选择要上传的文件,但是你不能使用selenium与它交互,而必须使用pyautogui。