我曾经使用以下python + selenium库代码来喜欢任何IG帖子。
下面是一段代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
"""
imagine on chrome browser you click on any IG picture of a gallery
of a specific account
then you see only that specific picture have the heart icon to click to like
see printscreen below
"""
like = browser.find_element_by_css_selector("[aria-label='Like']")
like.get_attribute("aria-label")
like.click() #error on this line !!!!!!
它以前是可以工作的,但最近我得到了以下错误:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <span aria-label="Like" class="glyphsSpriteHeart__filled__16__white u-__7"></span> is not clickable at point (827, 76). Other element would receive the click: <div class=" _32yJO" role="dialog">...</div>
(Session info: chrome=95.0.4638.69)
有人能帮我解决这个问题吗?
发布于 2021-11-23 01:10:40
该元素位于svg
标记中。为了与svg
标记交互,下面是语法。You can refer this.
//*[local-name()='svg']
Or
//*[name()='svg']
根据DOM的屏幕截图,Like
元素的xpath为:
//*[local-name()='svg' and @aria-label='Like']
一定要检查xpath是否是DOM中该元素的惟一定位器(1/1匹配)。You can refer this.
发布于 2021-11-23 03:14:24
要点击Instagram图标,您可以使用以下任一Locator Strategies
使用css_selector
的
button svgaria-label='Like'").click()“driver.find_element(By.CSS_SELECTOR
使用xpath
的
name(By.XPATH,"//button//*name()='svg‘和@aria-label='Like'").click()
所需的元素是一个动态元素,因此要单击需要为element_to_be_clickable()
引入WebDriverWait的元素,可以使用以下Locator Strategies之一
使用CSS_SELECTOR
的
driver(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button svgaria-label='Like'"))).click() (驱动程序,WebDriverWait“按钮)
使用XPATH
的
name(driver,20).until(EC.element_to_be_clickable((By.XPATH,“//button//*WebDriverWait()=‘svg’和@aria-label='Like'"))).click()
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
参考文献
您可以在下面的链接中找到一些相关的详细讨论:
发布于 2021-11-23 04:57:14
通常如下所示的xpath
//*[name()='svg' and @aria-label='Like']
应表示HTMLDOM中的所有链接图标。
如果你想更具体,即基于IG句柄,你想按like图标,那么你应该使用这个
//a[text()='barked']/ancestor::header/../../following-sibling::div[2]//descendant::span[2]//*[name()='svg']
此外,我还建议您使用ExplicitWait
driver WebdriverWait
wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.XPATH, "paste the above here"))).click()
导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
如果我们在HTML DOM
中有唯一的条目,请检查dev tools
(谷歌chrome)。
您应该检查的xpath:
//*[name()='svg' and @aria-label='Like']
要检查的步骤:
Press F12 in Chrome
->转到element
部分-> do a CTRL + F
->,然后粘贴xpath
并查看所需的element
是否正在使用 matching 1/1
matching节点进行高亮显示。
https://stackoverflow.com/questions/70074075
复制相似问题