我的硒脚本有问题。 我想对运行在javascript上的网站进行网络抓取。 我一直在互联网上收集很多信息,但是找不到解决方案。 picture of the html code picture of my code 在这篇文章中,我还提交了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)
答案 0 :(得分:0)
尝试一下:
#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()
答案 1 :(得分:0)
您应该wait将该元素加载到页面上,所以请使用:
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)