私は現在、Selenium Webdriverを使用してFacebookユーザーの友達ページを解析し、AJAXスクリプトからすべてのIDを抽出しています。しかし、私はすべての友達を取得するために下にスクロールする必要があります。Seleniumで下にスクロールするにはどうすればよいですか。私はPythonを使用しています。
あなたが使用することができます
driver.execute_script("window.scrollTo(0, Y)")
ここで、Yは高さです(フルHDモニターでは1080です)。(@lukeisに感謝します)
使用することもできます
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
ページの一番下までスクロールします。
ソーシャルネットワークやFacebookなど、読み込みが無限のページにスクロールしたい場合(@Cuong Tranに感謝)
SCROLL_PAUSE_TIME = 0.5
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
別の方法(Juanseのおかげで)は、オブジェクトを選択して
label.sendKeys(Keys.PAGE_DOWN);
(linkedin.comのように)無限のページの一番下までスクロールしたい場合は、次のコードを使用できます。
SCROLL_PAUSE_TIME = 0.5
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
element=find_element_by_xpath("xpath of the li you are trying to access")
element.location_once_scrolled_into_view
これは、表示されていない「li」にアクセスしようとしたときに役立ちました。
私の目的では、ウィンドウの位置を念頭に置いて、さらに下にスクロールしたいと思いました。私の解決策は似ていて、使用されましたwindow.scrollY
driver.execute_script("window.scrollTo(0, window.scrollY + 200)")
これは現在のyスクロール位置+200に移動します
これはあなたがウェブページを下にスクロールする方法です:
driver.execute_script("window.scrollTo(0, 1000);")
少なくともFacebookの検索結果ページを下にスクロールするためには、これらの答えはどれも私にはうまくいきませんでしたが、このソリューションを何度もテストした後、私は見つけました:
while driver.find_element_by_tag_name('div'):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
Divs=driver.find_element_by_tag_name('div').text
if 'End of Results' in Divs:
print 'end'
break
else:
continue
その問題を解決するために私が見つけた最も簡単な方法は、ラベルを選択してから送信することでした。
label.sendKeys(Keys.PAGE_DOWN);
それがうまくいくことを願っています!
ユーチューブで作業する場合、フローティング要素がスクロール高さとして値「0」を与えるようにではなく、使用して「document.body.scrollHeightを返す」、このいずれかを使用してみてください「document.documentElement.scrollHeightが返す」 あなたのインターネットごとにスクロール休止時間を調整しますそれ以外の場合は、1回だけ実行され、その後中断します。
SCROLL_PAUSE_TIME = 1
# Get scroll height
"""last_height = driver.execute_script("return document.body.scrollHeight")
this dowsnt work due to floating web elements on youtube
"""
last_height = driver.execute_script("return document.documentElement.scrollHeight")
while True:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0,document.documentElement.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.documentElement.scrollHeight")
if new_height == last_height:
print("break")
break
last_height = new_height
動的なWebページをスクロールし、ページの終わりに達すると自動的に停止する方法を探していたところ、このスレッドが見つかりました。
@Cuong Tranによる投稿は、主な変更点が1つあり、私が探していた答えでした。他の人が変更が役立つかもしれないと思ったので(それはコードの動作に顕著な影響を及ぼします)、したがってこの投稿。
変更は、ループ内の最後のページの高さをキャプチャするステートメントを移動することです(各チェックが前のページの高さと比較されるように)。
したがって、以下のコード:
動的なWebページ(
.scrollTo()
)を継続的に下にスクロールし、1回の反復でページの高さが同じままの場合にのみ停止します。
(別の変更があり、breakステートメントが別の条件(ページが「スティック」の場合)内にあり、削除できます)。
SCROLL_PAUSE_TIME = 0.5
while True:
# Get scroll height
### This is the difference. Moving this *inside* the loop
### means that it checks if scrollTo is still scrolling
last_height = driver.execute_script("return document.body.scrollHeight")
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
# try again (can be removed)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
# check if the page height has remained the same
if new_height == last_height:
# if so, you are done
break
# if not, move on to the next loop
else:
last_height = new_height
continue
このコードは一番下までスクロールしますが、毎回待つ必要はありません。継続的にスクロールしてから、一番下で停止します(またはタイムアウトします)
from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path='chromedriver.exe')
driver.get('https://example.com')
pre_scroll_height = driver.execute_script('return document.body.scrollHeight;')
run_time, max_run_time = 0, 1
while True:
iteration_start = time.time()
# Scroll webpage, the 100 allows for a more 'aggressive' scroll
driver.execute_script('window.scrollTo(0, 100*document.body.scrollHeight);')
post_scroll_height = driver.execute_script('return document.body.scrollHeight;')
scrolled = post_scroll_height != pre_scroll_height
timed_out = run_time >= max_run_time
if scrolled:
run_time = 0
pre_scroll_height = post_scroll_height
elif not scrolled and not timed_out:
run_time += time.time() - iteration_start
elif not scrolled and timed_out:
break
# closing the driver is optional
driver.close()
これは、応答が0.1秒かかる可能性がある場合に、応答を毎回0.5〜3秒待つよりもはるかに高速です。
読み込みページをスクロールします。例:ミディアム、クォーラなど
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight-1000);")
# Wait to load the page.
driver.implicitly_wait(30) # seconds
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
# sleep for 30s
driver.implicitly_wait(30) # seconds
driver.quit()
このScrollTo()
機能は動作しなくなりました。これは私が使用したものであり、正常に機能しました。
driver.execute_script("document.getElementById('mydiv').scrollIntoView();")
特定のビュー/フレーム(WebElement)内でスクロールする場合は、「body」を、スクロールする予定の特定の要素に置き換えるだけです。以下の例では、「getElementById」を介してその要素を取得します。
self.driver.execute_script('window.scrollTo(0, document.getElementById("page-manager").scrollHeight);')
これは、たとえばYouTubeの場合です...
このタイプの目的に使用できるセレンコードスニペットの例を次に示します。「Pythonチュートリアルを列挙する」のYouTube検索結果のURLに移動し、「Pythonチュートリアルを列挙する(2020)」というタイトルのビデオが見つかるまで下にスクロールします。
driver.get('https://www.youtube.com/results?search_query=enumerate+python')
target = driver.find_element_by_link_text('Enumerate python tutorial(2020).')
target.location_once_scrolled_into_view
driver.execute_script("document.getElementById('your ID Element').scrollIntoView();")
私の場合はうまくいっています。
send_keysを使用して、PAGE_DOWNキーの押下(通常はページをスクロールする)をシミュレートできます。
from selenium.webdriver.common.keys import Keys
html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.PAGE_DOWN)
特徴的なスターのコリン・エッグレスフィールドは、RomaDrama Liveでのスリル満点のファンとの出会いについて料理しました!加えて、大会での彼のINSPIREプログラム。
ノーザンエクスポージャーが90年代の最も人気のある番組の1つになった理由を確認するには、Blu-rayまたはDVDプレーヤーをほこりで払う必要があります。
ドミニカのボイリング湖は、世界で2番目に大きいボイリング湖です。そこにたどり着くまでのトレッキングは大変で長いですが、努力する価値は十分にあります。
ジョエル・マクヘイル、マイク・コルター(スクリーンショット:Netflix)「私の神よ、これは1つのことを変えます。」これは、ジョエル・マクヘイルとのジョエル・マクヘイルショーの最後のジョークです。リアリティ番組の嘲笑と寛大なスナキネスの時間は、なじみのある顔を見つけます。
画像経由:@pancakeparadox(Twitter)。1997年にポケモンシリーズが初公開されて以来、チームロケット(またはラテンアメリカではチームロケット)として知られる悪役のグループは、何度もアッシュに直面してきました。
画像:ゲッティ私たち全員が千年もの間生きていて、私たちの体が燃える風によってほこりと長引く悲鳴だけに押し流されたと考えるのは驚くべきことです。私たちがそうしていないことを除いて、それはトランプ政権の最初の週の終わりであり、驚くほど多くの恐ろしいことがすでに起こっています。
そして今、あることを手に入れていると思っていたが、まったく別のことをしてしまった男の話。CBSニュースは、彼女が「ミニブタ」であるという誤ったふりをしてエスターを養子にしたカナダ人のスティーブジェンキンスの心温まる物語をもたらします。これは、特にせいぜいゴールデンレトリバーまたはセントバーナードをストラップします。
Zendaya shared a sweet photo in honor of boyfriend Tom Holland's 26th birthday Wednesday
シーレン「Ms.JuicyBaby」ピアソンは、先月脳卒中で入院した後、「もう一度たくさんのことをする方法を学ばなければならない」ため、言語療法を受けていることを明らかにしました。
オスカー受賞者の世紀半ばの家には、3つのベッドルーム、2つのバス、オーシャンフロントの景色があります。
Bioscoutは、農家を運転席に置くという使命を負っています。Artesian(GrainInnovate)やUniseedと並んで、最新のシードラウンドでチームを支援できることをうれしく思います。問題真菌症による重大な作物の損失は、農民にとって試練であることが証明されています。
遠隔医療は、パンデミック後の時代では新しいものではなく、時代遅れの分野でもありません。しかし、業界を詳しく見ると、需要と供給の強力な持続可能性と、米国で絶え間ない革命となる強力な潜在的成長曲線を示しています。
2021年は、世界的なベンチャーキャピタル(VC)の資金調達にとって記録的な年でした。DealStreetAsiaによると、東南アジアも例外ではなく、この地域では年間で記録的な25の新しいユニコーンが採掘されました。
計算に対する私たちの欲求とムーアの法則が提供できるものとの間には、指数関数的に増大するギャップがあります。私たちの文明は計算に基づいています—建築と想像力の現在の限界を超える技術を見つけなければなりません。