按钮在selenium python中不可单击

mf98qq94  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(416)

我在一个网站上有一个按钮,但它没有点击,即使我尝试通过xpath、id、类和其他方式查找元素。
这就是按钮的元素代码

<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)
okxuctiv

okxuctiv1#

您需要以全屏模式启动浏览器:

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()

相关问题