Python脚本在控制台中运行,但作为脚本出错

时间:2017-07-23 15:40:30

标签: python python-2.7 selenium selenium-webdriver

我正在编写一个脚本,从我必须登录使用的网站中提取一些信息。我正在使用Python 2.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(&#39;ctl00$Banner1$ModuleList$ctl00$lnkModule&#39;,&#39;&#39;)"><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(&#39;ctl00$Banner1$ModuleList$ctl01$lnkModule&#39;,&#39;&#39;)"><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(&#39;ctl00$Banner1$ModuleList$ctl02$lnkModule&#39;,&#39;&#39;)"><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>

1 个答案:

答案 0 :(得分:2)

最可能发生的是,当您从bash运行脚本时,脚本运行得太快,并且在浏览器加载页面之前启动get_by_id操作,这会导致此错误。

正如@ murali-selenium建议的那样,你应该在开始寻找文档中的内容之前添加一些等待时间。

这可以通过这种方式实现:

#!/usr/bin/python
from selenium import webdriver
import time

wait_time_sec = 1

browser = webdriver.Firefox(firefox_binary='/usr/bin/firefox', executable_path="./geckodriver")

# Get to the login page
browser.get('https://example.com')
time.sleep(wait_time_sec)
browser.find_element_by_link_text('Application').click()
time.sleep(wait_time_sec)

# 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()
time.sleep(wait_time_sec)

# Open the application
try:
    browser.find_element_by_id('ctl00_Banner1_ModuleList_ctl01_lnkModule').click()
except:
    print('failed')

#browser.stop()