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

标签 javascript python selenium leaflet

我正在使用 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

如果任何人都可以进一步阐明为什么 ActionChain 方法可以工作而不会遇到重叠的点,那就太好了。

同样,为什么 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/

相关文章:

javascript - 使用 Javascript 请求阻止?

python - Sympy - 求解器的不可预测的行为

python - 将执行顺序与打印函数调用作为函数参数混淆

selenium - 隐式、显式和流利等待之间的区别

testing - Selenium WebDriver : How to wait for iframes to load completely? 可以点击 iframe 元素文本框 id = “div_4_1_2_1_1-lnk”

javascript - AngularJs ng-cloak 大页面上 CSS 显示问题 :none

javascript - Google Calendar 嵌套 JSON -- 无法读取未定义的属性 'length'

python - Seaborn 线图高 CPU;与 matplotlib 相比非常慢

node.js - 使用 Protractor webdriver-manager 运行一项任务后 Grunt 停止

javascript - 在谷歌地图中编辑并保存更新的多边形坐标