Python script runs in console, but errors display as script

I am working on a script to extract some information from a site that I have to use for login. I am using Python 2.7.12 and 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()

If I copy this code and paste it into the python console, it will work fine and go to the page I want. However, when I run the script from the terminal (bash on Linux Mint 18), it does not work. This removes the output with try and catch statements:

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"]

I don’t even know how to solve the problem. Any help?

Edit

Here is a snippet of the link I'm trying to select:          

            <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>
+4
source share
1 answer

, , script bash script , 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()
+2

Source: https://habr.com/ru/post/1682071/


All Articles