简体   繁体   English

使用 Selenium 我试图单击页面上具有相同类的所有元素,但它不起作用

[英]Using Selenium I am trying to click all elements on the page with the same class but it is not working

I have multiple elements on a page with the same code.我在一个页面上有多个元素具有相同的代码。 Here are two examples of two of the elements:以下是其中两个元素的两个示例:

<button _ngcontent-jqb-c516="" cdkmonitorelementfocus="" class="quick-action-button ng-star-inserted" title="Refresh now" pbi-focus-tracker-idx="10">
<mat-icon _ngcontent-jqb-c516="" role="img" class="mat-icon notranslate glyph-small pbi-glyph-font-face pbi-glyph-refresh mat-icon-no-color ng-star-inserted" aria-hidden="true" data-mat-icon-type="font" data-mat-icon-name="pbi-glyph-refresh"></mat-icon><!----><!----></button>

<button _ngcontent-jqb-c516="" cdkmonitorelementfocus="" class="quick-action-button ng-star-inserted" title="Refresh now" pbi-focus-tracker-idx="11">
<mat-icon _ngcontent-jqb-c516="" role="img" class="mat-icon notranslate glyph-small pbi-glyph-font-face pbi-glyph-refresh mat-icon-no-color ng-star-inserted" aria-hidden="true" data-mat-icon-type="font" data-mat-icon-name="pbi-glyph-refresh"></mat-icon><!----><!----></button>

I am trying to click on all of the elements on the page which share this similar code.我正在尝试单击页面上共享此类似代码的所有元素。 It looks like they all have the same class.看起来他们都有相同的班级。 I have decided to put it in a loop:我决定把它放在一个循环中:

refresh = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_Name, "mat-icon notranslate glyph-small pbi-glyph-font-face pbi-glyph-refresh mat-icon-no-color ng-star-inserted"))).click()

for x in range(0,len(refresh)):
    if refresh[x].is_displayed():
        refresh[x].click()

Unfortunately, when I run the code I get an error:不幸的是,当我运行代码时出现错误:

AttributeError: type object 'By' has no attribute 'CSS_Selector'

Any ideas on what the code should look like?.关于代码应该是什么样子的任何想法? Thanks in advance.提前致谢。

Here are the full xpaths for some of the elements:以下是一些元素的完整 xpath:

/html/body/div[1]/root/mat-sidenav-container/mat-sidenav-content/div/div/workspace-view/fluent-workspace/mat-sidenav-container/mat-sidenav-content/fluent-workspace-list/fluent-list-table-base/div/cdk-virtual-scroll-viewport/div[1]/div[1]/div[2]/span/button[1]/mat-icon

/html/body/div[1]/root/mat-sidenav-container/mat-sidenav-content/div/div/workspace-view/fluent-workspace/mat-sidenav-container/mat-sidenav-content/fluent-workspace-list/fluent-list-table-base/div/cdk-virtual-scroll-viewport/div[1]/div[2]/div[2]/span/button[1]/mat-icon

/html/body/div[1]/root/mat-sidenav-container/mat-sidenav-content/div/div/workspace-view/fluent-workspace/mat-sidenav-container/mat-sidenav-content/fluent-workspace-list/fluent-list-table-base/div/cdk-virtual-scroll-viewport/div[1]/div[3]/div[2]/span/button[1]/mat-icon

Interestingly, when I use this code:有趣的是,当我使用这段代码时:

refresh= driver.find_elements_by_class_name('quick-action-button ng-star-inserted')


for x in range(0,len(refresh)):
    if refresh[x].is_displayed():
        refresh[x].click()

I don't get any error but it doesn't click any elements.我没有收到任何错误,但它没有单击任何元素。

The class By doesn't have an attribute of CSS_Name . By类没有CSS_Name的属性。

Below is the attribute for the css selector from the selenium docs :下面是来自 selenium 文档的 css 选择器的属性:

...
CSS_SELECTOR = "css selector"

So the correction should be to change By.CSS_Name to By.CSS_SELECTOR所以更正应该是将By.CSS_Name更改为By.CSS_SELECTOR

暂无
暂无

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

相关问题 在Python中使用Selenium单击具有相同类名的所有元素 - Using Selenium in Python to click through all elements with the same class name 我正在尝试使用 Selenium 单击嵌套 div 中的按钮 - I am trying to click a button that is inside nested divs using Selenium 同时点击同一个class的所有元素 - Click on all elements of the same class at the same time 尝试单击网页中的所有HTML元素 - Trying to click all HTML elements in a web page 当有多个相同的 class 并且没有使用 Python 的 id 元素时,使用 selenium 创建点击事件 - Create a click event using selenium when there are multiple same class and no id elements using Python 我正在尝试 select 一个盒子中的图像,然后使用 selenium python 单击对齐按钮 - I am trying to select an image from a box and then click on the aligning button using selenium python 代码不起作用。 我试图点击页面上带有 id 号的元素,但我不能 - Code not working. I am trying to click an element on a page with its id number but i cant 使硒单击相同类的连续元素n次 - Make selenium click Consecutive Elements with same class for n times 如何使用 selenium 和 python 抓取数据,我正在尝试提取标题 div 标签中的所有数据 - How to scrape data using selenium and python, I am trying to extract all the data which is in title div tag Selenium 驱动程序不返回所有 web 元素,即使它们是相同的 class - Selenium driver not returning all web elements even though they are the same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM