我试图刮一页包含许多行数据的页面。除非在底部更改下拉列表,否则此数据分布在多个页面上,每页只有25行。
我查看了Selenium中的常规类和名称选择,它没有什么简单的。它有类名,但它们是同一个类中的多个名称,find_by_class_name将无法使用它们。
根据我的研究,我应该使用xpath但是我下面的所有尝试都失败了。
我也尝试过简单地更改页面但是在没有xpath的情况下按钮不可点击的同一问题出现了,xpath将无效。
我尝试从xpath扩展中收集Xpath以使其完全正确,Selenium仍然会抛出错误。
这是我必须点击的下拉列表的html片段
<select ng-model="ctrl.limit" ng-options="pageSize as pageSize for pageSize in ctrl.pageSizeOptions" ng-change="ctrl.onPageSizeChange()" class="ng-pristine ng-untouched ng-valid ng-not-empty" aria-invalid="false"><option label="25" value="number:25" selected="selected">25</option><option label="50" value="number:50">50</option><option label="100" value="number:100">100</option><option label="500" value="number:500">500</option></select>
这是它在视觉上的样子
这是我的扩展程序给我的当前xpath以及我如何将它提供给Selenium
select = Select(driver.find_element_by_xpath("/html[@class=' geolocation svg localstorage sessionstorage audio canvas canvastext video webgl no-emoji inlinesvg supports svgclippaths smil nthchild cssanimations csscolumns csscolumns-width csscolumns-span csscolumns-fill csscolumns-gap csscolumns-rule csscolumns-rulecolor csscolumns-rulestyle csscolumns-rulewidth csscolumns-breakbefore csscolumns-breakafter csscolumns-breakinside cssfilters flexbox flexboxlegacy']/body[@class='nav-menu-open']/blocking-notifications/span/ng-transclude/content-with-sidebar[@class='content-and-sidebar']/div[@class='flex-main page-main']/main[@class='flex-section page-content ntux-wrapper sidebar-open--ntux']/div[@class='tester-app view-contents--ntux']/ng-transclude/content-slot/div[@class='view-contents']/ui-view/div[@class='testcycle main-page-content is-ttl']/div[@class='tab-content']/div[@class='testcase-list main-page-content']/responsive-table/div/div[@class='responsive-table-wrap']/div[@class='responsive-table-pagination']/span/select[@class='ng-pristine ng-valid ng-not-empty ng-touched']"))
select.select_by_visible_text('500')
而错误是
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
预期 - 选择元素,可以使用其他命令打开
实际 - 我收到一条错误,指出该元素不存在
谢谢你的帮助!
您可以尝试以下代码,看看它是否有帮助。
select=Select(driver.find_element_by_css_selector("select.ng-pristine.ng-untouched.ng-valid.ng-not-empty"))
select.select_by_visible_text("500")
要么。
driver.find_element_by_css_selector("select.ng-pristine.ng-untouched.ng-valid.ng-not-empty").click()
driver.find_element_by_css_selector("select.ng-pristine.ng-untouched.ng-valid.ng-not-empty").send_keys(Keys.END)
driver.find_element_by_css_selector("select.ng-pristine.ng-untouched.ng-valid.ng-not-empty").send_keys(Keys.ENTER)