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.
from selenium import webdriver
browser = webdriver.Firefox(firefox_binary='/usr/bin/firefox', executable_path="./geckodriver")
browser.get('https://example.com')
browser.find_element_by_link_text('Application').click()
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()
try:
browser.find_element_by_id('ctl00_Banner1_ModuleList_ctl01_lnkModule').click()
except:
print('failed')
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('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>
source
share