gpt4 book ai didi

javascript - Seleneium 异常 - arguments[0].click 不是在 Selenium Python 中使用 execute_script() 的函数

转载 作者:行者123 更新时间:2023-12-04 08:56:21 25 4
gpt4 key购买 nike

我正在使用 Python 和 Selenium 在此网站 ( https://collegecrisis.shinyapps.io/dashboard/ ) 上抓取交互式 map 。具体来说,我想从 map 中获取在弹出窗口中找到的文本。
在第一次尝试使用 Scrapy 执行此操作后,我改变了策略,因为我正在寻找的信息位于弹出窗口中,并且 map 的可点击部分重叠,并且在循环浏览所有相关元素时并非都可以访问。
我收到异常:ElementClickInterceptedException 元素点击被拦截:元素在点 (391, 500) 处不可点击。其他元素将收到点击:
( session 信息:chrome=Xxxx)
因此,我找到了一些建议,尝试通过这种方法使用 javascript 与 map 交互:

driver.execute_script('arguments[0].click();', element)
但是,我收到了错误(在其他类似问题中,受访者强调该错误表明缺少括号和/或冒号,它们不在代码中,错误仅显示下面没有它们的代码):
JavascriptException:javascript 错误:arguments[0].click 不是函数
( session 信息:chrome=85.0.4183.83)
我试过使用 driver.execute_script('arguments[0].click();', element) 方法来查看这是否在 map 中有效。单击“缩放”按钮时会这样做:
zoom_btn = driver.find_element_by_class_name("leaflet-control-zoom-in")
driver.execute_script("arguments[0].click();", zoom_btn)
我可以使用 .click() 在 map 上单击单个点,而无需 javascript:
specific_node = driver.find_element_by_xpath('.//*[contains(@d, "M421.5685916444444,133.94770476315125a1,1 0 1,0 2,0 a1,1 0 1,0 -2,0 ")]')
specific_node.click()
但是我无法循环,因为 map 上的点重叠,所以点击问题顶部提到的每个元素(然后在弹出窗口中抓取文本)。
到目前为止我使用的代码如下。最后一个 for 循环未完成,因为我无法让点击部分工作。
# import packages
from selenium import webdriver
from selenium.common.exceptions import ElementClickInterceptedException

# setup drivers
PATH = "/Applications/chromedriver"
driver = webdriver.Chrome(PATH)
driver.implicitly_wait(10) # seconds
driver.get("https://collegecrisis.shinyapps.io/dashboard/")

# find all class elements =leaflet-interactive
nodes = driver.find_elements_by_class_name("leaflet-interactive")

# loop through clickable elements and get text from pop-up
for node in nodes:
node.click()
# get text from popups (code to be finished). node.find_elements_by_xpath('.//div[@class = "leaflet-popup-content"]')
使用 ActionChains 的可行解决方案:
# import packages
from selenium import webdriver
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep

# setup drivers
PATH = "/Applications/chromedriver"
driver = webdriver.Chrome(PATH)
driver.implicitly_wait(10) # seconds
driver.get("https://collegecrisis.shinyapps.io/dashboard/")

# find all class elements =leaflet-interactive
nodes = driver.find_elements_by_class_name("leaflet-interactive")

# use actionchains
nodelist = []

# loop through each node
for node in nodes:
ActionChains(driver).move_to_element(node).click().perform() # Used actionchains class to click to open popup
sleep(.5)
nodelist.append(driver.find_element_by_class_name('leaflet-popup-content').text)
ActionChains(driver).move_to_element(node).click().perform() #click to close popup
如果有人可以进一步说明为什么 行动链 方法在没有遇到重叠点的情况下工作,这将是很好的。
同样,为什么 没有? driver.execute_script('arguments[0].click();', element) 方法不行?

最佳答案

由于所有的点都相互重叠,因此最好使用 ActionChains 类方法来模拟在它们上方移动鼠标光标,然后单击:

ActionChains(driver).move_to_element(node).click().perform() # Used actionchains class to click to open popup
time.sleep(.5)
contents.append(driver.find_element_by_class_name('leaflet-popup-content').text)
ActionChains(driver).move_to_element(node).click().perform() #click to close popup

关于javascript - Seleneium 异常 - arguments[0].click 不是在 Selenium Python 中使用 execute_script() 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63810615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com