我想點擊一個按鈕,當你沒有這個網站的cookies時,這個按鈕會出現,名為 "accéder au site"。 以下是擁有這個按鈕的程序:
我正在使用Selenium(Python)。
這是一個網站。https://www.allocine.fr 我嘗試了很多方法,如css選擇器、部分鏈接文本、切換到框架,但都沒有效果。 我還認為我的問題是在按鈕出現之前必須等待,但這也不起作用。
這是我的代碼 :
from yggtorrentscraper importYggTorrentScraperSelenium
from selenium import webdriver
options = webdriver.ChromeOptions()
options.headless = False[/span]。
options.add_argument("--log-level=3")
prefs = {"profile.default_content_setting_values.notifications" : 2}。
options.add_experimental_option("prefs",prefs)
PATH_CHROME_DRIVER = "C:UsersKevinDesktopchromedriver.exe" 。
driver = webdriver.Chrome(PATH_CHROME_DRIVER, options=options)
driver.get("https://www.allocine.fr")
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/div/div/div[2]/div/span/div/button[2]'/span>) ).click()
#not working[/span]。
#WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div/a')) .click()
這里也是按鈕的HTML內容
<div id="dfp_interstitial">
<div id="dfp_interstitial__top">
<div id="dfp_interstitial__top-logo" style="background-image: url("https://lh3.googleusercontent.com/-PBjnn15gFS4/XJzvx16iKdI/AAAAAAAADro/yVpahUmuWwMxthTXopnE0lLgJ0zJxG0qgCK8BGAs/s0/2019-03-28.png");" > </div>
<div id="dfp_interstitial__top-close">
accéder au site <span class="dfp_interstitial__arrow-right"/span>> </span><span class="dfp_interstitial__arrow-right"> </span>
</div>
</div>
<a id="dfp_interstitial__click" target="_blank"/span>>
<img id="dfp_interstitial__image" src="https://tpc.googlesyndication.com/simgad/5287997328350019355?" style="max-width: 1280px;">
</a>
我真的不知道我做錯了什么。希望得到任何幫助!
uj5u.com熱心網友回復:
有幾件事 :
CSS優于xpath。請注意,你使用的是絕對的xpath,它在本質上是很脆弱的。使用明確的等待。
accéder au site是在一個iframe中,所以你必須先切換到iframe,然后你可以點擊這個按鈕。
代碼 :-
driver = webdriver.Chrome(driver_path)
driver.maximum_window()
driver.implicitly_wait(30)
wait = WebDriverWait(driver, 20)
driver.get("https://www.allocine.fr/")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick^='Didomi.setUserAgreeToAll']")).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[id^='google_ads_iframe'][id$='homepage/home_2']"))
wait.until(EC.element_to_be_clickable((By.ID, "dfp_interstitial__top-close") ).click()
匯入 :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/327080.html
標籤:
