Selenium不会移动到下一个元素。

huangapple go评论64阅读模式
英文:

Selenium doesn't move to the next element

问题

I am trying to make a instagrambot with selenium. I am trying to click post or story in the dropdown menu, when inspect(developer tools) is open, they don't work when I first click the create [+] button. In terminal it looks like it just pauses and don't move on to the next element, it doesn't give an error.

But when I click the create [+] button myself and wait for the "post" xpath to click on post, it works and opens the filedialog.

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()

Image: Selenium不会移动到下一个元素。

英文:

I am trying to make a instagrambot with selenium. I am trying to click post or story in the dropdown menu, when inspect(developer tools) is open, they don't work when I first click the create [+] button. In terminal it looks like it just pauses and don't move on to the next element, it doesn't give an error.

But when I click the create [+] button myself and wait for the "post" xpath to click on post, it works and opens the 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()

https://i.stack.imgur.com/ZINmk.png

答案1

得分: 0

以下是您要翻译的内容:

The usual .click() doesn't work for the [+] button: the script hangs on this command, don't know why, it may be that the driver thinks the page is not loaded yet. Then I tried with javascript and it works:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

create = WebDriverWait(driver, 9).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'nav a[role=link]')))

driver.execute_script("arguments[0].click()", create)

After this click a dropdown menu opens. I tried several methods to click on the "Post" button and finally found one that works:

post = WebDriverWait(driver, 9).until(EC.element_to_be_clickable((By.XPATH, '//div[text()="Post"]/ancestor::div[@role="button"]')))

from selenium.webdriver.common.keys import Keys
post.send_keys(Keys.RETURN)

## click but hang on
# post.click()

## no click and no hang on
# driver.execute_script("arguments[0].click()", post)

## no click and hang on
# from selenium.webdriver.common.action_chains import ActionChains
# actions = ActionChains(driver)
# actions.move_to_element(post).click().perform()

## no click and no hang on
# actions.move_to_element(post).send_keys(Keys.RETURN).perform()

## no click and hang on
# actions.click_and_hold(post).perform()
# time.sleep(1)
# actions.release(post).perform()

After this click a popup opens which lets you select the file to upload, however you cannot use selenium to interact with it but you have to use pyautogui.

英文:

The usual .click() doesn't work for the [+] button: the script hangs on this command, don't know why, it may be that the driver thinks the page is not loaded yet. Then I tried with javascript and it works:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

create = WebDriverWait(driver,9).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'nav a[role=link]')))

driver.execute_script("arguments[0].click()", create)

After this click a dropdown menu opens. I tried several methods to click on the "Post" button and finally found one that works:

post = WebDriverWait(driver,9).until(EC.element_to_be_clickable((By.XPATH, '//div[text()="Post"]/ancestor::div[@role="button"]')))

from selenium.webdriver.common.keys import Keys
post.send_keys(Keys.RETURN)

## click but hang on
# post.click()

## no click and no hang on
# driver.execute_script("arguments[0].click()", post)

## no click and hang on
# from selenium.webdriver.common.action_chains import ActionChains
# actions = ActionChains(driver)
# actions.move_to_element(post).click().perform()

## no click and no hang on
# actions.move_to_element(post).send_keys(Keys.RETURN).perform()

## no click and hang on
# actions.click_and_hold(post).perform()
# time.sleep(1)
# actions.release(post).perform()

After this click a popup opens which lets you select the file to upload, however you cannot use selenium to interact with it but you have to use pyautogui.

huangapple
  • 本文由 发表于 2023年2月6日 19:43:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定