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



我试图喜欢并且不像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

并遍历这个,在喜欢一个帖子后给了我一个相同的元素点击拦截异常错误

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

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

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

尝试通过 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)

相关内容

  • 没有找到相关文章

最新更新