我在Python 2.7中编写了一个受控制的sepage控制网页解析器。它在Chrome(v64)中运行良好并完成工作。但是,在Firefox(v58)中,网页打开非常正常,但是代码会在find_element_by_name
的第一个实例处生成错误。
我收到以下错误
Traceback (most recent call last):
File "generate_fastener list.py", line 13, in <module>
elem = br.find_element_by_name("userId")
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 487, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 955, in find_element
'value': value})['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 241, in check_response
raise exception_class(message, screen, stacktrace, alert_text)
selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None
Message:
代码的前几行如下:
import urllib
import os
import time
import fileinput
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from formatFilename import formatFilename
br = webdriver.Firefox()
br.get("some_url")
elem = br.find_element_by_name("userId")
elem.clear()
elem.send_keys("myname")
当我尝试查找表单元素&#34; userId&#34;时发生错误。我已将geckodriver放在/usr/local/bin
文件夹中。
答案 0 :(得分:0)
我的代码块中没有看到任何此类问题。这似乎是一个同步问题。一种可能的解决方案是使用以下任一选项将<{em> WebDriverWait expected_conditions
子句引导为element_to_be_clickable
:
使用XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@name='userId']"))).send_keys("myname")
使用CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[name*='userId']"))).send_keys("myname")
您需要以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC