[英]selenium python a click in a cookie doesn't work
i try to click a button cookie, but doesn't locate element.我尝试单击按钮 cookie,但没有找到元素。 with linux works but not with windows.
与 linux 一起工作,但不适用于 windows。
my code :我的代码:
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
options = Options()
options.add_argument("start-maximized")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'chromedriver.exe')
url = "https://www.lachainemeteo.com/meteo-france/previsions-meteo-france-aujourdhui"
driver.get(url)
sleep(2)
btn = driver.find_element(By.CLASS_NAME,"sc-1epc5np-0.dnGUzk.sc-f7uhhq-2.coEmEP.button.button--filled.button__acceptAll").click()
where is the probleme, thank you问题出在哪里,谢谢
Here's a command that worked for me:这是一个对我有用的命令:
driver.find_element(By.CSS_SELECTOR, "button.button__acceptAll").click()
I used By.CSS_SELECTOR
with "button.button__acceptAll"
.我将
By.CSS_SELECTOR
与"button.button__acceptAll"
一起使用。
Easy to verify from the console.易于从控制台验证。
For reliability on slower page loads, this is better:对于较慢的页面加载的可靠性,这是更好的:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
# ...
WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable(
(By.CSS_SELECTOR, "button.button__acceptAll")
)
).click()
Because this alert only appears for me when I set the browser's locale code to fr
, I would consider adding a try
/ except
block around the code.因为仅当我将浏览器的语言环境代码设置为
fr
时才会出现此警报,所以我会考虑在代码周围添加一个try
/ except
块。 Wait for the alert to appear for up to 5 seconds, and if it does, close it, otherwise continue:等待警报出现最多 5 秒钟,如果出现,请将其关闭,否则继续:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
# ...
try:
WebDriverWait(self.driver, 5).until(
EC.element_to_be_clickable(
(By.CSS_SELECTOR, "button.button__acceptAll")
)
).click()
except Exception:
pass
with just this line and it's ok, thx for your help Michael , it's work now.仅此行就可以了,感谢您的帮助Michael ,现在可以使用了。 with my system linux chrome is in english, and windows my chrome in french, you are god
我的系统 linux chrome 是英文的,windows 我的 chrome 是法文的,你是上帝
options.add_argument("--lang=en")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.