python - 如何修复硒,元素不可交互?
问题描述
所以我正在尝试创建一个程序来解决用户输入的复杂数学问题。我正在使用网站 mathpapa 来解决这些方程。
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
PATH = 'C:\Program Files\chromedriver.exe'
message = input("Enter a math problem: ")
web = webdriver.Chrome(PATH)
web.get('https://www.mathpapa.com/algebra-calculator.html')
equation = web.find_element_by_xpath('//*[@id="source3"]/span[2]')
equation.send_keys(message + Keys.RETURN)
answer = WebDriverWait(web, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="solout3"]/div[2]')))
print(answer.text)
我在线上遇到错误equation = web.find_element_by_xpath('//*[@id="source3"]/span[2]')
错误是说元素不可交互,有没有其他方法可以将消息输入到 mathpapa 上的文本栏中?
解决方案
您的代码有两个问题:
- 您正试图在页面完全加载之前访问方程式输入元素。
这就是您收到该错误的原因。
您必须在此之前添加一个等待。
我还更改了定位器以更精确。
equation = WebDriverWait(web, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#source3 textarea')))
equation.send_keys(message + Keys.RETURN)
- 此外,您为答案元素使用了错误的定位器。
尝试这个:
answer = WebDriverWait(web, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div#solout3 mn')))
print(answer.text)
推荐阅读
- javascript - 试图从数据属性中找到 id 并设置它
- javascript - 单击a时如何进行多个ajax调用
- 元素
- arrays - 在 bash 中使用递增/递减
- docker - 为什么“Docker run”每次都会创建新容器?
- java - Java如何避免在循环中使用Thread.sleep()
- php - 发布 php,向用户显示页面一次
- php - 在 PHP 中删除 CSV 的第一行?
- python - 即使表达式为假,python 仍然执行 elif 的主体
- python-3.x - 在 Python3 中使用加密模块加载 openssl 私钥时无法反序列化密钥数据错误
- c++ - mac 漂亮打印 gdb