简体   繁体   English

Selenium点击下一页直到最后一页

[英]Selenium clicking to next page until on last page

I am trying to keep clicking to next page on this website , each time appending the table data to a csv file and then when I reach the last page, append the table data and break the while loop 我试图继续点击这个网站的下一页,每次将表数据附加到csv文件,然后当我到达最后一页时,附加表数据并打破while循环

Unfortunately, for some reason it keeps staying on the last page, and I have tried several different methods to catch the error 不幸的是,由于某种原因,它一直停留在最后一页,我尝试了几种不同的方法来捕获错误

while True:
  try :
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, 'Next'))).click()
  except :
      print("No more pages left")
      break
driver.quit()

I also tried this one: 我也试过这个:

try:
    link = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'.pagination-next a')))
    driver.execute_script('arguments[0].scrollIntoView();', link)
    link.click()
except:
    keep_going = False

I've tried putting print statements in and it just keeps staying on the last page. 我已经尝试过将print语句放入其中并且它一直停留在最后一页。

Here is the HTML for the first page/last page for the next button, I'm not sure if I could do something utilizing this: HTML for first page: 这是下一个按钮的第一页/最后一页的HTML,我不确定我是否可以利用这个来做一些事情:第一页的HTML:

<li role="menuitem" ng-if="::directionLinks" ng-class="{disabled: noNext()||ngDisabled}" class="pagination-next" style=""><a href="" ng-click="selectPage(page + 1, $event)" ng-disabled="noNext()||ngDisabled" uib-tabindex-toggle="">Next</a></li>
    <a href="" ng-click="selectPage(page + 1, $event)" ng-disabled="noNext()||ngDisabled" uib-tabindex-toggle="">Next</a>
</li>

HTML for last page: 最后一页的HTML:

<li role="menuitem" ng-if="::directionLinks" ng-class="{disabled: noNext()||ngDisabled}" class="pagination-next disabled" style=""><a href="" ng-click="selectPage(page + 1, $event)" ng-disabled="noNext()||ngDisabled" uib-tabindex-toggle="" disabled="disabled" tabindex="-1">Next</a></li>
    <a href="" ng-click="selectPage(page + 1, $event)" ng-disabled="noNext()||ngDisabled" uib-tabindex-toggle="" disabled="disabled" tabindex="-1">Next</a>
</li>

You can solve the problem as below, 您可以解决以下问题,

Next Button Will be enabled until the Last Page and it will be disabled in the Last Page. 下一个按钮将一直启用,直到最后一页,它将在最后一页中被禁用。

So, you can create two list to find the enabled button element and disabled button element. 因此,您可以创建两个列表来查找已启用的按钮元素和已禁用的按钮元素。 At any point of time,either enabled element list or disabled element list size will be one.So, If the element is disabled, then you can break the while loop else click on the next button. 在任何时候,启用元素列表或禁用元素列表大小都将为1。因此,如果元素被禁用,则可以中断while循环,否则单击下一个按钮。

I am not familiar with python syntax.So,You can convert the below java code and then use it.It will work for sure. 我不熟悉python语法。所以,你可以转换下面的java代码,然后使用它。它肯定会工作。

Code: 码:

    boolean hasNextPage=true;

    while(hasNextPage){
        List<WebElement> enabled_next_page_btn=driver.findElements(By.xpath("//li[@class='pagination-next']/a"));
        List<WebElement> disabled_next_page_btn=driver.findElements(By.xpath("//li[@class='pagination-next disabled']/a"));

        //If the Next button is enabled/available, then enabled_next_page_btn size will be one.
        // So,you can perform the click action and then do the action
        if(enabled_next_page_btn.size()>0){
            enabled_next_page_btn.get(0).click();
            hasNextPage=true;
        }else if(disabled_next_page_btn.size()>0){
            System.out.println("No more Pages Available");
            break;
        }
    }

The next_page_btn.index(0).click() wasn't working, but checking the len of next_page_btn worked to find if it was the last page, so I was able to do this. next_page_btn.index(0).click()无法正常工作,但检查next_page_btn的len工作是为了查找它是否是最后一页,所以我能够做到这一点。

while True:
    next_page_btn = driver.find_elements_by_xpath("//li[@class = 'pagination-next']/a")
    if len(next_page_btn) < 1:
        print("No more pages left")
        break
    else:
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, 'Next'))).click()

Thanks so much for the help! 非常感谢你的帮助!

How about using a do/while loop and just check for the class "disabled" to be included in the attributes of the next button to exit out? 如何使用do / while循环并检查“禁用”类是否包含在下一个按钮的属性中以退出? (Excuse the syntax. I just threw this together and haven't tried it) (请原谅语法。我只是把它扔到一起而没试过)

string classAttribute

try :

     do
     {
          IWebElement element = driver.findElement(By.LINK_TEXT("Next"))
          classAttribute = element.GetAttribute("class")
          element.click()
     }
     while(!classAttribute.contains("disabled"))

except :

     pass

driver.quit()

xPath to the button is: xPath到按钮是:

//li[@class = 'pagination-next']/a

so every time you need to load next page you can click on this element: 所以每次你需要加载下一页时你都可以点击这个元素:

next_page_btn = driver.find_elements_by_xpath("//li[@class = 'pagination-next']/a")
next_page_btn.index(0).click()

Note: you should add a logic: 注意:您应该添加一个逻辑:

while True:
    next_page_btn = driver.find_elements_by_xpath("//li[@class = 'pagination-next']/a")
    if len(next_page_btn) < 1:
        print("No more pages left")
        break
    else:
        # do stuff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM