Selenium python - 元素在点不可点击
Posted
技术标签:
【中文标题】Selenium python - 元素在点不可点击【英文标题】:Selenium python - element is not clickable at point 【发布时间】:2016-04-06 11:26:22 【问题描述】:我正在尝试在 Instagram 帖子页面中获取关注按钮文本(页面示例 - https://www.instagram.com/p/BD2C8pQwO8X/)
driver = webdriver.Chrome()
driver.set_window_size(1024, 768)
driver.get('https://www.instagram.com/accounts/login/')
driver.implicitly_wait(10)
username_field = driver.find_element_by_name('username')
password_field = driver.find_element_by_name('password')
username_field.send_keys(user_login)
password_field.send_keys(user_pass)
password_field.send_keys(Keys.RETURN)
time.sleep(5)
def find_tag():
search_field = driver.find_element_by_xpath('//nav/div/div/div/div/input')
search_field.send_keys('#moscow')
time.sleep(1)
driver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div/div/div/div[1]/div[2]/div[2]/div/a[1]').click()
def follow_from_tag():
top_20_posts = driver.find_elements_by_xpath('//article/div/div/div/a')
for post in top_20_posts:
post.click()
time.sleep(1.5)
print(driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/article/header/span/button').text)
find_tag()
time.sleep(2)
follow_from_tag()
但出现错误:
selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (511, 415). Other element would receive the click: <a class="_c2kdw" href="javascript:;" role="button" data-reactid=".1.1.0.0.1.$https=2//scontent-arn2-1=1cdninstagram=1com/t51=12885-15/e15/12479233_260398184295103_1013244176_n=1jpg?ig_cache_key=0MTIyMjM0Nzg4MzM1MDYzNTQzOA%3D%3D=12.0.0.1"></a>
(Session info: chrome=49.0.2623.110)
(Driver info: chromedriver=2.9.248307,platform=Mac OS X 10.10.5 x86_64
我做错了什么?
【问题讨论】:
似乎另一个元素与您要单击的元素重叠。你在哪一行得到错误? 文件“/Users/kr/PycharmProjects/education_py/instagram.py”,第 24 行,在 follow_from_tag post.click() 中 看看这个:***.com/a/19763087/1033737 我运行了你的代码,//article/div/div/div/a
似乎不正确,你想得到什么?
@KonstantinRusanov,你想从最后一页得到什么?
【参考方案1】:
这得到你想要的:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def wait(dr, x):
element = WebDriverWait(dr, 5).until(
EC.presence_of_element_located((By.XPATH, x))
)
return element
driver = webdriver.Chrome()
driver.set_window_size(1024, 768)
driver.get('https://www.instagram.com/accounts/login/')
driver.implicitly_wait(10)
username_field = driver.find_element_by_name('username')
password_field = driver.find_element_by_name('password')
username_field.send_keys("user")
password_field.send_keys("pass")
password_field.send_keys(Keys.RETURN)
def find_tag():
search_field = driver.find_element_by_xpath('//nav/div/div/div/div/input')
search_field.send_keys('moscow')
wait(driver, '//*[@id="react-root"]/section/nav/div/div/div/div[1]/div[2]/div[2]/div/a[1]').click()
def follow_from_tag():
top_20_posts = [a.get_attribute("href") for a in
driver.find_elements_by_xpath("//a[contains(@href,'?tagged=moscow')]")[:20]]
for href in top_20_posts:
driver.get(href)
print(href)
btn = wait(driver, "//button[text()='Follow']")
# btn.click() # uncomment to follow
打印输出:
https://www.instagram.com/p/BD249tsxEbl/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79810>
https://www.instagram.com/p/BD2puxzELOu/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79790>
https://www.instagram.com/p/BD2jrmyP8ec/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79750>
https://www.instagram.com/p/BD2v9NyvzIs/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79810>
https://www.instagram.com/p/BD2qkcZm8cg/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79790>
https://www.instagram.com/p/BD2sEWzLlEF/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79750>
https://www.instagram.com/p/BD25VA2vI6t/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79810>
https://www.instagram.com/p/BD2fRXoB1t2/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79790>
https://www.instagram.com/p/BD2jYlfE0Yw/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79750>
https://www.instagram.com/p/BD2s0PtoRP1/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79810>
https://www.instagram.com/p/BD2mn_QBQPK/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79790>
https://www.instagram.com/p/BD2zTmqM-5h/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79750>
https://www.instagram.com/p/BD2q6Qfqc5q/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79810>
https://www.instagram.com/p/BD2iivvDbiO/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79790>
https://www.instagram.com/p/BD2-WU2msdO/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79750>
https://www.instagram.com/p/BD2Qa2Rn0GV/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79810>
https://www.instagram.com/p/BD2ffCZDR8L/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79790>
https://www.instagram.com/p/BD2kqDhwywb/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79750>
https://www.instagram.com/p/BD2r4M7lf-h/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79810>
https://www.instagram.com/p/BD2t3PTAlJC/?tagged=moscow
<selenium.webdriver.remote.webelement.WebElement object at 0x7fa717d79790>
您可能想要添加错误处理并使用更好的 xpath,但它可以满足您的需求,如果您想关注,只需取消注释 btn.click()
。
我错过了你想查的文字,如果你想查的文字,我们可以用包含"Follow"
作为匹配词:
def follow_from_tag():
top_20_posts = [a.get_attribute("href") for a in
driver.find_elements_by_xpath("//a[contains(@href,'?tagged=moscow')]")[:20]]
for href in top_20_posts:
driver.get(href)
print(href)
btn = wait(driver, "//button[contains(text(),'Follow')]")
print(btn.text)
我随机选择一个跟随后的输出:
https://www.instagram.com/p/BD23RGrkLEN/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD2jrmyP8ec/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD249tsxEbl/?tagged=moscow
FOLLOWING
https://www.instagram.com/p/BD2v9NyvzIs/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD2qkcZm8cg/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD2sEWzLlEF/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD25VA2vI6t/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD2fRXoB1t2/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD2s0PtoRP1/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD2jYlfE0Yw/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD2mn_QBQPK/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD2zTmqM-5h/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD2-WU2msdO/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD2q6Qfqc5q/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD2iivvDbiO/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD29pcrDs6o/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD2Qa2Rn0GV/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD2ffCZDR8L/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD2kqDhwywb/?tagged=moscow
FOLLOW
https://www.instagram.com/p/BD2r4M7lf-h/?tagged=moscow
FOLLOW
所以你只需要一个if btn.text.lower() == "follow"
等等。
【讨论】:
【参考方案2】:您发布的网址不包含//*[@id="react-root"]/section/nav/div/div/div/div[1]/div[2]/div[2]/div/a[1]
。但是我假设当将(虚拟)鼠标光标从您的 search_field
移动到您要单击的元素时,它会在页面上产生一些 MouseOver 效果,在某种意义上,会弹出一个叠加层或其他东西,将可点击元素隐藏在后面自己。
我遇到了同样的问题并这样解决了: How to avoid MouseOver on Selenium Click()
【讨论】:
那部分确实有效。它会将您带到https://www.instagram.com/explore/tags/moscow/
,它与发布的网址位于完全不同的页面上
是的,这部分将我带到带有莫斯科标签的热门帖子,但是在我打开每个帖子并想要打印跟随按钮文本之后以上是关于Selenium python - 元素在点不可点击的主要内容,如果未能解决你的问题,请参考以下文章
如何错误处理 Selenium 无法单击元素,因为其他元素在 C# 中遮挡了它? [复制]
selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截:元素不可点击 Selenium 和 Python
selenium.common.exceptions.ElementNotInteractableException:消息:使用 Selenium Python 单击元素时元素不可交互