【问题标题】:How do I click on this item using Selenium?如何使用 Selenium 单击此项目?
【发布时间】:2022-01-19 01:01:51
【问题描述】:

我正在尝试使用 selenium 自动下载报告。要访问报告所在的页面,我必须单击带有此代码的图像

<div class="leaflet-marker-icon single-icon-container running hover asset leaflet-zoom-hide leaflet-clickable" tabindex="0" style="margin-left: -22px; margin-top: -41px; width: 44px; height: 44px; opacity: 1; transform: translate3d(525px, 238px, 0px); z-index: 238;"><div class="icon-value" lid="219058"></div></div>

我试过了

wtg = driver.find_elements_by_class_name(
    "leaflet-marker-icon single-icon-container running hover asset leaflet-zoom-hide leaflet-clickable")
wtg.click()

但什么也没发生。 有 7 个元素具有相同的类,并且唯一的“id”看起来像 lid="219058",但我不知道如何选择它。

【问题讨论】:

    标签: python selenium selenium-webdriver css-selectors selenium-chromedriver


    【解决方案1】:

    一般来说,构建网络爬虫的最佳实践是始终使用 xpath,因为 xpath 可以以更灵活的方式应用所有过滤器(id、class 等)(但在某些情况下,selenium 的性能可能会降低)。

    我建议你查看这篇关于如何编写 xpath 以满足各种需求的文章:https://www.softwaretestinghelp.com/xpath-writing-cheat-sheet-tutorial-examples/

    对于您的特定用例,我会使用:

    driver.find_element_by_xpath('//div[@lid="219058"]')
    

    这实际上会点击内部 div(注意盖子实际上是如何在嵌套 div 内的)。如果你想点击外部 div,你可以使用:

    driver.find_element_by_xpath('//div[@lid="219058"]/parent::div')
    

    我再次建议您学习 Xpath 语法并始终使用它,它比其他 selenium 选择器更容易操作,并且在您选择实现 C 编译的 html 解析器(例如 lxml)来解析元素时也更快.

    【讨论】:

    • 从技术上讲,XPATH 是最慢的定位器,并且每个浏览器的引擎都不同,这使得它非常脆弱。我总是推荐 ID,NAME 如果它在那里,但 CSS _SELECTORS 更快,更兼容。
    • 显然你是正确的@ArundeepChohan,很抱歉这个错误。由于我习惯使用可以很好地处理 xpath 的 lxml(这是一个更快的解析器),因此我正在编辑响应。
    【解决方案2】:

    记住driver.find_elements_by_class_name() 返回一个list

    在使用这个get/find方法时你必须做这样的事情:

    driver.find_elements_by_class_name('class')[0] #If you want the first of the page
    

    在您的情况下,您需要使用css_selector,因为您有多个类,就像@Prophet 建议的那样。 您也可以只使用其中一个类并简单地使用class_name 选择器。

    在您的情况下,如果您需要具有该类的页面的第一个元素,则必须添加 [0]

    【讨论】:

    • 我收到此错误NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".leaflet-marker-icon.single-icon-container.running.hover.asset.leaflet-zoom-hide.leaflet-clickable"}
    【解决方案3】:

    leaflet-marker-icon single-icon-container running hover asset leaflet-zoom-hide leaflet-clickable 包含多个类名,而driver.find_element_by_class_name 方法旨在获取单个类名。
    由于您没有共享页面链接,因此我无法为您提供此元素的正确定位器,但是如果您希望根据这些类名组合定位该元素,您可以使用 CSS 选择器或 XPath,如下所示:

    wtg = driver.find_element_by_css_selector(".leaflet-marker-icon.single-icon-container.running.hover.asset.leaflet-zoom-hide.leaflet-clickable")
    wtg.click()
    

    或者

    wtg = driver.find_element_by_xpath("//*[@class='leaflet-marker-icon single-icon-container running hover asset leaflet-zoom-hide leaflet-clickable']")
    wtg.click()
    

    您还应该使用driver.find_element_by_class_name,而不是driver.find_elements_by_class_name,因为driver.find_elements_by_class_name 会给您一个网络元素列表,而不是一个可以直接单击的网络元素。
    或者,您可以使用接收到的 web 元素列表中的第一个索引,如 FLAK-ZOSO 所述

    【讨论】:

    • 谢谢,但没用。页面是voe.vestas.com,但是需要密码才能进入。
    猜你喜欢
    • 2019-06-10
    • 2020-09-23
    • 2022-12-04
    • 2022-01-07
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2018-10-31
    相关资源
    最近更新 更多