我正在尝试编写一个脚本,允许我以编程方式向新闻网站提交评论。 我正在使用Selenium,这是我的纸条(与我尝试使用的链接完全一致):
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
url = "https://www.delfi.lt/en/lifestyle/earth-day-events-for-the-spring-equinox.d?id=87005127"
driver.get(url)
# Clicking 'I agree' on a cookies banner:
cookies_ok = '//*[@id="c-right"]/a'
driver.find_element_by_xpath(cookies_ok).click()
# XPath list
anon = '//*[@id="comments-listing"]/div[2]/div/div[2]/div/ul/li[1]/span'
name = '//*[@id="inputDiv"]/div/form/input'
comment = '//*[@id="inputDiv"]/div/form/div[3]/div/textarea'
button = '//*[@id="inputDiv"]/div/form/div[4]/div[2]/button[1]'
# Click 'Anonymous' -> fill name and comment fields -> press PUBLISH
driver.find_element_by_xpath(anon).click()
driver.find_element_by_xpath(name).send_keys('name')
driver.find_element_by_xpath(comment).send_keys('comment')
driver.find_element_by_xpath(button).click()
一切正常,但当我执行最后一个命令时,我在网站上收到以下消息:
“浏览器阻止或不支持cookie”。然而,当我自己在浏览器中执行相同的步骤时,cookie没有问题
关于如何防止这个错误有什么想法吗
谢谢
您可以优化代码,我假设您必须使用最新的二进制文件:
试试这个
这将安装最新的chrome浏览器并运行测试
您可能需要使用
pip install webdriver-manager
安装webdriver-manager
相关问题 更多 >
编程相关推荐