selenium 中的按钮不可点击 python
Button is not clickable in selenium python
我在网站上有一个按钮,但即使我尝试了 find_element_by_xpath、id、class 和其他按钮,它也没有点击。
按钮的元素代码是这样的
<button type="button" class="gpbutton" id="gtltranslate">Translate</button>
这是我的代码,它打开一个文件并将它们转换成另一个文件
import os
from tkinter import *
from tkinter import filedialog
import selenium
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium import webdriver
from selenium import webdriver
import contextlib as textmanager
PATH= "C:\Program Files (x86)\chromedrivers\chromedriver.exe"
driver= webdriver.Chrome(PATH)
eng=[]
driver.get("https://www.webtran.eu/finnish_english_translator/")
input1= driver.find_element_by_id("gtlsource")
output1= driver.find_element_by_id("gtlresults_body")
btn= driver.find_element_by_class_name("gpbutton")
lines= []
with open("sanat.txt", "r") as file:
for line in file:
line = line.strip() #or some other preprocessing
lines.append(line) #storing everything in memory!
k=0
while k != len(lines):
input1.send_keys(lines[k])
time.sleep(0.5)
btn.click()
time.sleep(0.5)
eng.append(output1)
input1.clear()
k=k+1
time.sleep(1)
这是错误
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button type="button" class="gpbutton" id="gtltranslate">...</button> is not clickable at point (534, 490). Other element would receive the click: <span id="cookieconsent:desc" class="cc-message">...</span>
(Session info: chrome=91.0.4472.124)
您需要以全屏模式启动浏览器:
driver.maximize_window()
然后像这样使用下面的 css 选择器:
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#gtltranslate"))).click()
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
PS : 你可以在你的代码中像这样使用屏幕最大化 :
driver= webdriver.Chrome(PATH)
driver.maximize_window()
driver.get("https://www.webtran.eu/finnish_english_translator/")
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.cc-compliance a"))).click()
我在网站上有一个按钮,但即使我尝试了 find_element_by_xpath、id、class 和其他按钮,它也没有点击。
按钮的元素代码是这样的
<button type="button" class="gpbutton" id="gtltranslate">Translate</button>
这是我的代码,它打开一个文件并将它们转换成另一个文件
import os
from tkinter import *
from tkinter import filedialog
import selenium
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium import webdriver
from selenium import webdriver
import contextlib as textmanager
PATH= "C:\Program Files (x86)\chromedrivers\chromedriver.exe"
driver= webdriver.Chrome(PATH)
eng=[]
driver.get("https://www.webtran.eu/finnish_english_translator/")
input1= driver.find_element_by_id("gtlsource")
output1= driver.find_element_by_id("gtlresults_body")
btn= driver.find_element_by_class_name("gpbutton")
lines= []
with open("sanat.txt", "r") as file:
for line in file:
line = line.strip() #or some other preprocessing
lines.append(line) #storing everything in memory!
k=0
while k != len(lines):
input1.send_keys(lines[k])
time.sleep(0.5)
btn.click()
time.sleep(0.5)
eng.append(output1)
input1.clear()
k=k+1
time.sleep(1)
这是错误
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button type="button" class="gpbutton" id="gtltranslate">...</button> is not clickable at point (534, 490). Other element would receive the click: <span id="cookieconsent:desc" class="cc-message">...</span>
(Session info: chrome=91.0.4472.124)
您需要以全屏模式启动浏览器:
driver.maximize_window()
然后像这样使用下面的 css 选择器:
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#gtltranslate"))).click()
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
PS : 你可以在你的代码中像这样使用屏幕最大化 :
driver= webdriver.Chrome(PATH)
driver.maximize_window()
driver.get("https://www.webtran.eu/finnish_english_translator/")
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.cc-compliance a"))).click()