如何通过html元素uisng python selenium for循环单击页面中的每个元素?

时间:2019-09-02 05:17:15

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

我想喜欢和不喜欢Instagram feed页面中的所有帖子。我喜欢第一个帖子,却找不到找到喜欢多个帖子的方法。我正在使用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")

那么,我该如何喜欢Feed中显示的所有帖子?任何建议都会有所帮助,谢谢

1 个答案:

答案 0 :(得分: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)