【问题标题】:How to for loop though html element uisng python selenium to click each element in a page?如何使用python selenium循环遍历html元素以单击页面中的每个元素?
【发布时间】:2020-01-05 04:24:37
【问题描述】:

我正在尝试喜欢和不喜欢 Instagram 提要页面中存在的所有帖子。我能够喜欢第一个帖子,但无法找到喜欢多个帖子的方法。我正在使用 XPath 来获取该页面的元素

comment_like_xpath="//button/span[@class='glyphsSpriteHeart__outline__24__grey_9 u-__7' and @aria-label='Like']"
comment_unlike_xpath="//button/span[@class='glyphsSpriteHeart__filled__24__red_5 u-__7' and @aria-label='Unlike']"

我可以使用下面的 xpath 点击单个第一篇文章

comment_like_elem = wait.until(EC.visibility_of_element_located((By.XPATH,comment_like_xpath))).click()

如果我使用页面中存在的元素列表,我会收到异常错误

comment_like_elem = browser.find_elements_by_xpath(comment_like_xpath)
comment_like_elem_len = len(comment_like_elem)#no  of liked elemets

如果我遍历元素列表,我会得到一个异常错误

for element in  comment_like_elem:   
    element.click()

每个元素都是

<selenium.webdriver.remote.webelement.WebElement (session="53072af595e97e0c84dfadcabbe3b44e", element="97c695b9-1ae0-4a37-a0fd-5ff5a52a2b1f")>

我在 element.click() 处遇到异常

selenium.common.exceptions.ElementClickInterceptedException:消息: 元素点击被拦截:元素在点 (487, 743) 处不可点击。其他 元素会收到点击:...(会话信息:chrome=76.0.3809.132)

点赞按钮元素如下

<button class="dCJp8 afkep _0mzm-"><span class="glyphsSpriteHeart__outline__24__grey_9 u-__7" aria-label="Like"></span></button>

即使我尝试使用这个 xpath

//*[@id="react-root"]/section/main/section/div[2]/div[1]/div/article[1]/div[2]/section[1]/span[1]/button/span

在喜欢单个帖子后,遍历它会给我一个相同的 ElementClickInterceptedException 错误

我为登录页面编写的代码如下所示

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.common.exceptions import TimeoutException, NoSuchElementException, ElementClickInterceptedException
from selenium.webdriver.common.action_chains import ActionChains
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument('--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1')

browser = webdriver.Chrome('path of chromedriver', options = options)
browser.set_page_load_timeout(30)
wait = WebDriverWait(browser, 30)
try:
    browser.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
except TimeoutException:
    print('continue')
    browser.close()
    exit()
time.sleep(3)
login_elem_usr = browser.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div/div/div/form/div[4]/div/label/input')
login_elem_usr.click()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']"))).send_keys("username")
login_elem_pwd = browser.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div/div/div/form/div[5]/div/label/input')
login_elem_pwd.click()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']"))).send_keys("password")
login_elem_login = browser.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div/div/div/form/div[7]/button/div')
login_elem_login.click()
time.sleep(5)
try:
    login_elem_popup = wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[3]/div/div/div[3]/button[2]")))#.click()
    login_elem_popup.click()
except TimeoutException:
    print("contine")
try:
    filterButtonElement = wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[3]/div/div/div[3]/button[2]")))
    filterButtonElement.click()
except TimeoutException:
    print("contine")

那么我怎样才能喜欢我的提要中的所有帖子?任何建议都会有所帮助,谢谢

【问题讨论】:

  • 你能发一张网站的图片吗?从我在 Instagram 复仇者联盟中看到的内容来看,这些按钮是不可见的,除非我先点击图片
  • @RegCent 我正在通过移动版页面打开页面。我已经更新了代码看看代码
  • 好像你的元素在某个加载元素的后面,你是否试图等待重叠元素不可见,你是否也试图滚动到该元素?
  • @SpencerMelo 是的,我尝试了它们都延迟并向下滚动页面我得到了同样的错误

标签: python python-3.x selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

尝试通过js发送点击事件。也许对你有帮助

comment_like_elem = wait.until(EC.presence_of_all_elements_located((By.XPATH,comment_like_xpath)))
for item in comment_like_elem:
    browser.execute_script("arguments[0].click()", item)

【讨论】:

  • 感谢您的解决方案完美运行。有效。你能解释一下你是怎么做到的吗?
  • @deepinside,我认为,这篇文章可以解释 js 脚本在 selenium 中是如何工作的(尤其是这种情况)。乐于助人。 link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-04
  • 2012-01-04
  • 2021-11-08
  • 1970-01-01
  • 2012-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多