简体   繁体   English

如何在 Python(Selenium)中单击此 HTML 元素

[英]How to click on this HTML element in Python(Selenium)

Recently, I used selenium to crawl some information on the website.最近用selenium爬取了网站上的一些信息。 And What I want to do is just clicking 'previous month button'.而我想要做的只是点击“上个月按钮”。

So I push 'F12' button on Chrome And find HTML Code like this所以我按下 Chrome 上的“F12”按钮并找到 HTML 代码像这样在此处输入图像描述

So I write code like this所以我写这样的代码

webDriver.find_element_by_css_selector('span.ui-icon ui-icon-circle-triangle-w').click()

But it doesn't work, I changed multiple times changing the code like this但它不起作用,我改变了多次改变这样的代码

webDriver.find_element_by_css_selector('a.ui-icon ui-icon-circle-triangle-w').click()
webDriver.find_element_by_css_selector('ui-icon ui-icon-circle-triangle-w').click()
webDriver.find_element_by_class_name('ui-icon ui-icon-circle-triangle-w').click()

Any of those don't work.这些都不行。 When I tried to use first one of three trial(that is: webDriver.find_element_by_css_selector('a.ui-icon ui-icon-circle-triangle-w').click()), the error pops up like this当我尝试使用三个试验中的第一个(即:webDriver.find_element_by_css_selector('a.ui-icon ui-icon-circle-triangle-w').click())时,错误弹出如下

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"a.ui-icon ui-icon-circle-triangle-w"}
 (Session info: chrome=86.0.4240.198)

I don't know why it doesn't work..我不知道为什么它不起作用..

Please help请帮忙

try using,尝试使用,

webDriver.find_element_by_class_name("ui-icon.ui-icon-circle-triangle-w").click()

whenever you see space in HTML code you should put '.'每当您在 HTML 代码中看到空格时,您应该输入“。” instead of space while writing with selenium.用 selenium 写入时代替空格。 Also, instead of writing class name just copy paste and replace space with '.'此外,不要写 class 名称,只需复制粘贴并将空格替换为“。” That is lot easier and removes tying errors.这更容易并且消除了绑定错误。

As per the HTML :根据HTML

html

The <span> tag is having the class attribute value set as ui-icon ui-icon-circle-triangle-w . <span>标签的class属性值设置为ui-icon ui-icon-circle-triangle-w

When you intent to use this class attribute value in a you can keep it as it is as follows:当您打算在中使用此class属性值时,您可以按如下方式保留它:

//span[@class='ui-icon ui-icon-circle-triangle-w']

When you intent to use this class attribute value in a you need to seperate the values through a dot (.) character as follows:当您打算在中使用此class属性值时,您需要通过点(.)字符分隔值,如下所示:

span.ui-icon.ui-icon-circle-triangle-w

Solution解决方案

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :理想情况下,要单击需要为element_to_be_clickable()诱导WebDriverWait的元素,您可以使用以下任一Locator Strategies

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     WebDriverWait(webDriver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.ui-icon.ui-icon-circle-triangle-w"))).click()
  • Using XPATH :使用XPATH

     WebDriverWait(webDriver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='ui-icon ui-icon-circle-triangle-w']"))).click()
  • Note : You have to add the following imports:注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

References参考

You can find a couple of relevant discussions on NoSuchElementException in:您可以在以下位置找到一些关于NoSuchElementException的相关讨论:

webDriver.find_element_by_css_selector('span.ui-icon ui-icon-circle-triangle-w')

will find element which is a child element of span tag and had the class "ui-icon-circle-triangle-w"将找到作为 span 标签的子元素并且具有class "ui-icon-circle-triangle-w" 的元素

if you want to check the span tag that have the class "ui-icon ui-icon-circle-triangle-w"如果您想检查具有 class "ui-icon ui-icon-circle-triangle-w" 的 span 标签

you have to use '.'你必须使用'。' in css to refer multiple classes.在 css中引用多个类。 As spaces in html class indicates end of a class so ui-icon and ui-icon-circle-triangle-w are two differen classes由于 html class 中的空格表示 class 的结尾,因此 ui-icon 和 ui-icon-circle-triangle-w 是两个不同的类

the equalent locator is:相等的定位器是:

webDriver.find_element_by_css_selector('span.ui-icon.ui-icon-circle-triangle-w')

means span tag which has the classes "ui-icon" and "ui-icon-circle-triangle-w"表示具有“ui-icon”和“ui-icon-circle-triangle-w”类的 span 标签

  1. in css locator space is equalent to '//' in xpath css 中的定位符空间等于 xpath 中的“//”
  2. IN CSS locator > is equalent to '/' in xpath IN CSS 定位符 > 等于 xpath 中的“/”

Also see below thread:另请参阅下面的线程:

The below thread shows the full discussion on 'Element not found': https://sqa.stackexchange.com/q/41860/40022以下线程显示了关于“未找到元素”的完整讨论: https://sqa.stackexchange.com/q/41860/40022

The summary is to:总结是:

1. Trust your code and doubt SUT (Software under test): 1. 相信你的代码并怀疑 SUT(被测软件):

If everything was working fine and the test starts to fail suddenly.如果一切正常并且测试突然开始失败。 Instead of debugging your code for issues, start with checking the actual product.与其调试代码的问题,不如从检查实际产品开始。 Do some visual inspection and see whether the development team has modified the element or the element is no longer being displayed.做一些视觉检查,看看开发团队是否修改了元素或元素不再显示。

2. Trust your code and doubt the environment: 2.相信你的代码,怀疑环境:

If everything was working fine locally and failed as soon as you integrated to CI/CD.如果一切都在本地工作正常并且在您集成到 CI/CD 后立即失败。 Then investigate the the product behavior in test server.然后调查测试服务器中的产品行为。 Mostly due to OS and configuration difference product won't work as it would in local ( Raise a Bug)主要是由于操作系统和配置差异,产品无法像在本地那样工作(引发错误)

3. Now doubt your scripts (Using absolute XPATH): 3. 现在怀疑你的脚本(使用绝对 XPATH):

You might be using absolute XPATH, this causes flaky tests when DOM structure changes.您可能正在使用绝对 XPATH,这会在 DOM 结构更改时导致不稳定的测试。 Use relative XPATH (CSS would be more recommended).使用相对的 XPATH(更推荐使用 CSS)。 Never use xpath/ if you have unique ID/name to identify an element.如果您有唯一的 ID/名称来标识元素,请不要使用 xpath/。

4. Now doubt your scripts (Not using explicit wait): 4. 现在怀疑你的脚本(不使用显式等待):

Sometimes scripts lacks explicit wait and try to interact with dynamic elements, this causes test to fail because it try to interact with the element before it is even available in DOM.有时脚本缺乏显式等待并尝试与动态元素交互,这会导致测试失败,因为它甚至在元素在 DOM 中可用之前就尝试与元素交互。

5. Now doubt your scripts (Handling spinners): 5. 现在怀疑你的脚本(处理微调器):

Sometimes spinner takes time to get displayed.有时微调器需要时间才能显示。 SO, if you are just checking for the invisibility condition then it will be return true and try to interact with next element before the actual spinner event completes所以,如果您只是检查不可见条件,那么它将返回 true 并尝试在实际微调器事件完成之前与下一个元素交互

So, first check visibility of spinner and then check for invisibility before interacting with other dynamic elements.因此,首先检查微调器的可见性,然后在与其他动态元素交互之前检查不可见性。

6. Now doubt your scripts (Not handling iFrames): 6. 现在怀疑你的脚本(不处理 iFrame):

Sometimes element will be inside iframes and scripts won't switch between frames before interacting with these elements.有时元素会在 iframe 中,并且脚本在与这些元素交互之前不会在帧之间切换。

Check whether, any parent element contains the tag frame or iframe to determine whether element is inside an iframe检查任何父元素是否包含标签框或 iframe 以确定元素是否在 iframe 内

7. Now doubt your scripts (Not disabling wait for angular): 7.现在怀疑你的脚本(不禁用等待角度):

Sometimes products uses spinners to wait for asynchronous operations to complete behind the scene.有时产品使用微调器在后台等待异步操作完成。 For example you click login and spinner comes up and will not disappear till the background tasks are not completed.例如,您单击登录,然后微调器出现,并且在后台任务未完成之前不会消失。

In this case make sure you are not waiting for the asynchronous operations to complete (eg waitforangular flag set to true in protractor) before interacting with the elements in the temporary overlay.在这种情况下,请确保在与临时叠加层中的元素进行交互之前,您没有等待异步操作完成(例如,量角器中的 waitforangular 标志设置为 true)。

This is because, if you set waitforangular to true, then scripts wait till all the tasks are completed and by then the temporary overlay (say spinner) will be removed from DOM这是因为,如果你将 waitforangular 设置为 true,那么脚本会等到所有任务完成,然后临时覆盖(比如微调器)将从 DOM 中删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM