硒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)chromedriverschromedriver.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()

相关内容

  • 没有找到相关文章

最新更新