簡體   English   中英

使用 Selenium 我試圖單擊頁面上具有相同類的所有元素,但它不起作用

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

我在一個頁面上有多個元素具有相同的代碼。 以下是其中兩個元素的兩個示例:

<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>

我正在嘗試單擊頁面上共享此類似代碼的所有元素。 看起來他們都有相同的班級。 我決定把它放在一個循環中:

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()

不幸的是,當我運行代碼時出現錯誤:

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

關於代碼應該是什么樣子的任何想法? 提前致謝。

以下是一些元素的完整 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

有趣的是,當我使用這段代碼時:

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()

我沒有收到任何錯誤,但它沒有單擊任何元素。

By類沒有CSS_Name的屬性。

下面是來自 selenium 文檔的 css 選擇器的屬性:

...
CSS_SELECTOR = "css selector"

所以更正應該是將By.CSS_Name更改為By.CSS_SELECTOR

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM