我正在编写一个脚本,从一个我必须登录才能使用的站点中提取一些信息。我正在使用Python2.7.12和Selenium 3.4.3。你知道吗
#!/usr/bin/python
from selenium import webdriver
browser = webdriver.Firefox(firefox_binary='/usr/bin/firefox', executable_path="./geckodriver")
# Get to the login page
browser.get('https://example.com')
browser.find_element_by_link_text('Application').click()
# Login
browser.find_element_by_id('username').send_keys('notmyusername')
browser.find_element_by_id('password').send_keys('notmypassword')
browser.find_element_by_css_selector('.btn').click()
# Open the application
try:
browser.find_element_by_id('ctl00_Banner1_ModuleList_ctl01_lnkModule').click()
except:
print('failed')
#browser.stop()
如果我复制这段代码并将其粘贴到python控制台中,它就可以正常运行并转到我想要的页面。但是,当我从终端运行脚本(Linux Mint 18上的bash)时,它会出错。以下是删除try和catch语句后的输出:
Traceback (most recent call last):
File "./housing.py", line 15, in <module>
browser.find_element_by_id('ctl00_Banner1_ModuleList_ctl01_lnkModule').click()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 289, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 791, in find_element
'value': value})['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="ctl00_Banner1_ModuleList_ctl01_lnkModule"]
我都不知道怎么解决这个问题。有什么帮助吗?你知道吗
以下是我试图选择的链接片段: 你知道吗
<li>
<a id="ctl00_Banner1_ModuleList_ctl00_lnkModule" class="should-show ui-should-show header-4" AutoSetting="false" data-role="button" href="javascript:__doPostBack('ctl00$Banner1$ModuleList$ctl00$lnkModule','')"><span>Link A</span></a>
<noscript>
<input type="submit" name="ctl00$Banner1$ModuleList$ctl00$btnModule" value="Link A" id="ctl00_Banner1_ModuleList_ctl00_btnModule" class="header-4 button-as-link" />
</noscript>
</li>
<li>
<a id="ctl00_Banner1_ModuleList_ctl01_lnkModule" class="should-show ui-should-show" AutoSetting="false" data-role="button" href="javascript:__doPostBack('ctl00$Banner1$ModuleList$ctl01$lnkModule','')"><span>Link B</span></a>
<noscript>
<input type="submit" name="ctl00$Banner1$ModuleList$ctl01$btnModule" value="Link B" id="ctl00_Banner1_ModuleList_ctl01_btnModule" class="button-as-link" />
</noscript>
</li>
<li>
<a id="ctl00_Banner1_ModuleList_ctl02_lnkModule" class="should-show ui-should-show" AutoSetting="false" data-role="button" href="javascript:__doPostBack('ctl00$Banner1$ModuleList$ctl02$lnkModule','')"><span>Link C</span></a>
<noscript>
<input type="submit" name="ctl00$Banner1$ModuleList$ctl02$btnModule" value="Link C" id="ctl00_Banner1_ModuleList_ctl02_btnModule" class="button-as-link" />
</noscript>
</li>
</ul>
最可能发生的情况是,当您从bash运行脚本时,脚本运行得太快,
get_by_id
操作在浏览器加载完页面之前启动,从而导致此错误。你知道吗正如@murali selenium所建议的,在开始查找文档中的内容之前,您可能应该添加一些等待时间。你知道吗
可以通过以下方式实现:
相关问题 更多 >
编程相关推荐