[英]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.