Selenium异常参数[0]。click不是在Selenium Python中使用execute_script()的函数

2024-05-26 21:52:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python和Selenium在这个网站(https://collegecrisis.shinyapps.io/dashboard/)上创建一个交互式地图。具体来说,我想从地图的弹出窗口中找到文本

在第一次尝试使用Scrapy实现这一点后,我改变了策略,因为我正在寻找的信息在弹出窗口中,地图的可点击部分重叠,并且在遍历所有相关元素时无法全部访问

我得到了一个异常:元素ClickInterceptedException元素ClickIntercepted:元素在点(391500)处不可单击。其他元素将收到单击: (会话信息:chrome=Xxxx)

因此,我找到了一些建议,可以尝试通过此方法使用javascript与地图交互:

driver.execute_script('arguments[0].click();', element)

但是,我得到了错误(在其他类似问题中,受访者强调错误表明括号和/或冒号丢失,它们不在代码中,错误只是显示下面没有括号和/或冒号的代码): JavascriptException:javascript错误:参数[0]。单击不是函数 (会话信息:chrome=85.0.4183.83)

我尝试过使用驱动程序。执行_脚本('arguments[0]。click();',element)方法来查看这在映射中是否有效。单击“缩放”按钮时会执行此操作:

zoom_btn = driver.find_element_by_class_name("leaflet-control-zoom-in")
driver.execute_script("arguments[0].click();", zoom_btn)

我可以使用.click()在不使用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()

但我不能循环,点击每个元素(然后抓取弹出窗口中的文本),就像问题顶部提到的那样,因为地图上的点重叠

我目前使用的代码如下。最后一个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方法在没有遇到重叠点的情况下工作,那将是非常好的

类似地,驱动程序.execute_脚本('arguments[0]。click();',element)方法为什么不起作用


Tags: tofromimportnode元素bydriverselenium
1条回答
网友
1楼 · 发布于 2024-05-26 21:52:33

由于所有点相互重叠,最好使用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

相关问题 更多 >

    热门问题