【发布时间】:2020-09-04 17:42:11
【问题描述】:
我正在尝试单击带有 selenium webdriver 的按钮。但无法识别按钮。 这是我的代码:它返回一条错误消息
NoSuchElementException: 消息: u'no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="artifactContentList"]/div[2]/div /div[3]/content-dataset-actions/content-dataset-actions/section/section/button[2]"}\n(会话信息:headless chrome=81.0.4044.138)\n(驱动程序信息:chromedriver=2.39 .562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9),platform=Windows NT 10.0.18363 x86_64)'
from selenium.webdriver.chrome.options import Options
from splinter import Browser
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import csv
chrome_options = Options()
chrome_options.add_argument("--headless")
browser = webdriver.Chrome(executable_path= "C:\chromedriver\chromedriver.exe", chrome_options=chrome_options)
def open_url():
browser = webdriver.Chrome(executable_path= "C:\chromedriver\chromedriver.exe", chrome_options=chrome_options)
browser.get("https://app.powerbi.com/groups/xxxxs")
python_button = browser.find_element_by_xpath('/html/body/div[1]/root-downgrade/mat-sidenav-container/mat-sidenav-content/div/landing/div/div/div/ng-transclude/landing-route/list-proxy/content-list/div/main/dataset-list/div/virtual-scroll/div[2]/div/div[3]/content-dataset-actions/content-dataset-actions/section/section/button[2]')
print(python_button)
python_button.click()
browser.quit()
open_url()
这是完整的元素:
<button class="refreshNow pbi-glyph pbi-glyph-refresh" localize-tooltip="RefreshNow" ng-if="::$ctrl.canRefreshNow" ng-click="$ctrl.runAction($ctrl.RefreshNow)" aria-describedby="ModeldatasetMenu4" use-tooltip-as-aria-label="" title="Refresh now" aria-label="Refresh now" pbi-focus-tracker-idx="54"></button>
【问题讨论】:
-
find_elements 将返回对 DOM 对象的引用列表。 (点击方法将在列表中的每个项目上可用。也许您只需要 find_element 代替?
-
谢谢!这只是给我一个“没有这样的元素”的错误。我想我需要另一种方法来识别元素
-
我猜想通过有效凭据登录所需的元素。你有一套演示凭证吗?
-
我会将您的 XPATH 缩短为类似“//button[contains(@title, 'Refresh')]”的内容
-
问题是使用凭据登录。
标签: selenium