title='this is the title'
我要使用python / selenium在网页中的以下行中定位:
<input id="subject" name="subject" maxlength="50" value="" class="nude error" type="text">
我将此代码与python-selenium(在Debian下)一起使用:
title = driver.find_element_by_id("subject").clear()
title.send_keys(title)
我遇到以下错误:
Traceback (most recent call last):
File "./basic0", line 49, in <module>
titre.send_keys(title)
AttributeError: 'NoneType' object has no attribute 'send_keys'
注意:当脚本由于此错误而停止时,鼠标光标在网页内的右侧;但是我找不到找到send_keys填写输入内容的方法
我也尝试过:
title = driver.find_element_by_xpath("div[contains(text(),'subject')]")
title = driver.find_element_by_xpath("//form[input/@id='subject']")
title = driver.find_element_by_xpath("//form[input[@name='subject']")
但是它不起作用;此外,鼠标光标不在正确的位置。
然后我尝试了更高版本的硒:
我完全清除了Debian下的python-selenium软件包(这是硒v。2.53) 然后
pip install selenium==3.3.1
这一次,当我启动脚本时,它说geckodriver丢失了: 所以,
wget https://github.com/mozilla/geckodriver/releases/download/v0.23.0/geckodriver-v0.23.0-linux32.tar.gz
tar -xvzf geckodriver-v0.23.0-linux32.tar.gz
chmod 755 geckodriver (I also tried 777)
mv geckodriver /usr/local/bin/ (so it's in my PATH)
现在,当我启动脚本时,这是我收到的错误消息:
Traceback (most recent call last):
File "./basic0", line 13, in <module>
driver = webdriver.Firefox()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 155, in __init__
keep_alive=True)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 238, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refuse
firefox窗口弹出,然后在脚本停止时关闭
答案 0 :(得分:2)
您已分配了方法调用(clear()
,该方法调用将None
返回到title
变量,而您需要定义WebElement并随后将方法调用为
title = driver.find_element_by_id("subject")
title.clear()
title.send_keys("title")
答案 1 :(得分:0)
如果您能够在文本框中单击,但是由于send_keys无法使用并且很忙,或者只想单击而不选择任何元素,则无法在其中键入内容,
请通过键入 pip install pyautogui
import pyautogui
#use this line to type something
pyautogui.typewrite('Anything you wanna type')
#use this line to press any key
pyautogui.press("esc")
[here][1] is the list of keys you can press
Note : If you are going to minimize the windows pyautogui will start typing at the place you currently clicking or when the pyautogui.typewite() is executing and you are on other pages it will start typing on the page you are in rather then typing on webpage.