Selenium+Python:next按钮through click()

2024-03-28 20:48:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我不明白为什么当我翻到这页的时候

https://www.ryanair.com/it/it/voli-low-cost/?from=BGY&out-from-date=2018-09-01&out-to-date=2018-09-30&budget=60&trip-length-from=1&trip-length-to=3

或者不管该网站的哪个页面,Selenium都不允许我在脚本上运行带有以下代码的“下一页”按钮:

next_page = browser.find_element_by_xpath('/html/body/div[2]/main/div/div[2]/farefinder-widget/div/div[2]/div[2]/div/div/core-pagination/div/div[2]/a')
next_page.click()

在HTML代码上:

<a ng-class="{'disabled': $ctrl.isLastPage($ctrl.currentPageIndex)}" ng-click="$ctrl.handleNextClick()" class="core-link"><span class="nav-label">Successivo</span><core-icon icon-id="glyphs.chevron-right" class="icon-22 fill-ryanair-bright-blue"><div><svg tabindex="-1" focusable="false" role="img"><use xlink:href="/it/it/voli-low-cost/?from=BGY&amp;out-from-date=2018-09-01&amp;out-to-date=2018-09-30&amp;budget=60&amp;trip-length-from=1&amp;trip-length-to=3#glyphs.chevron-right" ng-href="/it/it/voli-low-cost/?from=BGY&amp;out-from-date=2018-09-01&amp;out-to-date=2018-09-30&amp;budget=60&amp;trip-length-from=1&amp;trip-length-to=3#glyphs.chevron-right"></use></svg></div></core-icon></a>

然而,如果我把它写在shell上,奇怪的是,它确实起作用了,有相同的行。我也试着用time.sleep给它一些时间,但是什么也没发生。你知道吗


Tags: tofromcoredivdateitoutlength
1条回答
网友
1楼 · 发布于 2024-03-28 20:48:30

此元素的分页似乎已禁用,即<a ng-class="{'disabled': $ctrl.isLastPage....。如果我单击父div,它对我有效,如下所示:

browser.find_element_by_css_selector(".next.vertical-center").click()

或者,如果要使用xpath,只需删除最后一个“a”:

next_step = browser.find_element_by_xpath('/html/body/div[2]/main/div/div[2]/farefinder-widget/div/div[2]/div[2]/div/div/core-pagination/div/div[2]')

为了可读性,我推荐CSS选择器。你知道吗

编辑:您可能会遇到异常,因为元素不可见。只需将元素滚动到视图中。你知道吗

element = browser.find_element_by_css_selector(".next.vertical-center")
browser.execute_script("arguments[0].scrollIntoView();", element)
element.click() 

相关问题 更多 >