Seleniumを使用して、このWebサイトの[csvをダウンロード]ボタンにアクセスしようとしています。https://fplreview.com/team-planner/#forecast_table。最初にサイトをクリックしたときに、「チームID」を入力して[送信]をクリックする必要があります。これで問題ありませんが、ポップアップ広告が表示され、閉じることができません。主にXPATHを使用していくつかのアプローチを試しましたが、ページが読み込まれるのを待つためにスリープタイマーを追加しても、ボタンがないというメッセージが表示されます。最終目標はリクエストを使用してこれを行うことですが、機能させようとしています。最初にSeleniumを使用します。ありがとう。以下は私のコードです
`
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://fplreview.com/team-planner/#forecast_table')
team_id = driver.find_element_by_name('TeamID')
team_id.send_keys(123)
team_id.submit()
# click close button on ad.
ad_path = '//*[@id="butt"]/html/body/div[2]/div[3]/div/div/div/div[1]/div/article/div[2]/div/div[18]/div/div/div[3]/button'
button = driver.find_element_by_xpath(ad_path)
button.click()
# export csv
export_button = driver.find_element_by_id('exportbutton')
export_button.click()
driver.quit()
`
これが生成するエラー
`
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="butt"]/html/body/div[2]/div[3]/div/div/div/div[1]/div/article/div[2]/div/div[18]/div/div/div[3]/button"}
`
それを機能させるには、いくつかの待機が必要です。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get('https://fplreview.com/team-planner/#forecast_table')
team_id = driver.find_element_by_name('TeamID')
team_id.send_keys(123)
team_id.submit()
# wait for the ad to load
WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.ID, 'orderModal_popop')))
# hide the ad
driver.execute_script("jQuery('#orderModal_popop').modal('hide');")
# export csv
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div[3]/div/div/div/div[1]/div/article/div[2]/div/button[6]')))
export_button = driver.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div/div[1]/div/article/div[2]/div/button[6]')
export_button.click()
# wait for download
time.sleep(3)
driver.quit()
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加