简体   繁体   English

使用python在网页上单击“加载更多”

[英]Clicking “Load More” on webpages by using python

I am trying to load the whole content of this link (clicking on المزيد) 我正在尝试加载此链接的全部内容(点击المزيد)

I tried so many tutorials like this one here , which teaches how to work with a similar issue (Infinite Scrolling Pages). 在这里尝试了很多这样的教程,教会如何使用类似的问题(无限滚动页面)。

My issue is that I couldn't manage to specify the load more class on this page to click it. 我的问题是我无法在此页面上指定加载更多类来单击它。 But, if I am not mistaken, it exists in this part of the webpage source code: 但是,如果我没有弄错的话,它存在于网页源代码的这一部分:

<ng-include src="'loader.html'" class="ng-scope">
    <div class="loading-div ng-scope ng-hide" ng-show="!loadingMoreData">
        <div class="spinner">
            <div class="bounce1"></div>
            <div class="bounce2"></div>
            <div class="bounce3"></div>
        </div>
    </div>
</ng-include>
<div class="block-hed block-link clearfix" data-ng-show="isMoreDisplayed" data-ng-click="getMoreMaterials()">
                    <h4 class="link-border-color"><a href="javascript:void(0)">المزيد </a></h4>
                </div>

I do not necessarily need to implement any function like "click()" or "perform()". 我不一定需要实现“click()”或“perform()”之类的任何功能。 Any way to show the whole content under load more button is considered. 任何方式来显示加载下的整个内容更多按钮被考虑。


by the way, this is my code so far: 顺便说一句,这是我的代码到目前为止:

from selenium import webdriver
browser = webdriver.Chrome("/home/aziz/anaconda3/lib/python3.6/site-packages/chromedriver/chromedriver")
am = browser.get("https://sabq.org/%D8%A7%D9%84%D9%85%D9%85%D9%84%D9%83%D8%A9/%D8%A5%D9%82%D8%AA%D8%B5%D8%A7%D8%AF")

UPDATE I used this link here to solve the issue, but none of the tutorials worked. 更新我在这里使用此链接来解决问题,但没有一个教程工作。

find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector

I finally managed to solve the problem. 我终于设法解决了这个问题。 I just added this line after my code 我刚刚在我的代码之后添加了这一行

while True:
    browser.find_elements_by_link_text('المزيد')[1].click()

and the page started infinitely loading all of the articles. 页面开始无限加载所有文章。 I didn't really know that المزيد itself is clickable. 我真的不知道المزيد本身是可点击的。 I thought there is a link included in it. 我认为其中包含一个链接。

The correct/better way to do it is to use WebDriverWait 正确/更好的方法是使用WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
while True:
  element = wait.until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, 'المزيد')))
  element.click()

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

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