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

2024-10-03 11:24:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个脚本,从一个我必须登录才能使用的站点中提取一些信息。我正在使用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(&#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>

Tags: browseridbyvalueusrseleniumelementfind
1条回答
网友
1楼 · 发布于 2024-10-03 11:24:53

最可能发生的情况是,当您从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()

相关问题 更多 >