簡體   English   中英

selenium python javascript單擊按鈕

[英]selenium python javascript click button

我的selenium腳本有問題。 我想webscrape一個運行在JavaScript上的網站。 我一直在互聯網上收集大量信息,但我找不到解決方案。 我的代碼 的html代碼 圖片的圖片在這篇文章中,我還提交了HTML代碼的打印屏幕。 基本上:我想加載網站時點擊接受按鈕,但我無法想象如何做到這一點。

在不同的網站上搜索解決方案。

你們可以幫助我使用我的腳本,我一直在嘗試和測試很多,但我無法理解。 謝謝。

碼:

from config import keys
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time


def order(k):

    chrome_path = 
    r"C:\Users\ltewo\PycharmProjects\livebetting\chromedriver.exe"
    driver = webdriver.Chrome(chrome_path)
    driver.get(keys['url'])
    xpath_button_accept = "//div[@class='cookieButtons']//a[@class='button 
    accept']"
    button_accept = driver.find_element_by_xpath(xpath_button_accept)
    xpath_button_accept.click()


if __name__ == '__main__':
    order(keys)

嘗試這個:

#update
driver.implicity_wait(10)


xpath_button_accept = "//div[@class='cookieButtons']//a[@class='button accept']"

button_accept = your_browser.find_element_by_xpath(xpath_button_accept)
button_accept.click()

您應該等待該元素加載到頁面上,因此請使用:

WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.XPATH, 'XpathOfMyElement')))

我注意到cookie面板位於iframe內部並且無法直接訪問,我已經更新了下面的內容,請檢查它現在是否正常工作。

更新了您的代碼:

from config import keys
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time


def order(keys):

    chrome_path = r"C:\Users\ltewo\PycharmProjects\livebetting\chromedriver.exe"
    driver = webdriver.Chrome(chrome_path)
    driver.get(keys['url'])
    driver.switch_to.frame('r42CookieBar')
    button_accept = driver.find_element_by_class_name('accept')
    button_accept.click()

if __name__ == '__main__':
     order(keys)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM