Working on removing boring stuff from my job. Below you find the Python's Selenium library being used to access the website containing German companies' registers. It does the following:
- opens the Handelsregister website,
- looks for the "normal search" button and presses it,
- looks for the field where company's name has to be input,
- input chosen company's name,
- changes the search manner to "contain the exact name of the company" and presses the "find" button,
- presses the "CD" button (CD is a type of register).
Then the site requires you to login in order to download the register, so the last thing you should see is the login page. I show you the error first, if I do it at the bottom it shows up as part of the code.
Traceback (most recent call last): File "C:/Users/adria/.PyCharmCE2019.1/config/scratches/Handelsregister_login_downloadCD.py", line 28, in choose_class_CD_0 = chrome.find_element(by=By.CLASS_NAME, value='RegPortErg') File "C:\Users\adria\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element 'value': value})['value'] File "C:\Users\adria\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Users\adria\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".RegPortErg"} (Session info: chrome=78.0.3904.108)
Now, how is it possible that the following code works, while it doesn't once I remove the #-commented strings, giving away the above error?
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
chrome = webdriver.Chrome(executable_path=r"C:\Users\...\Desktop\chromedriver_win32\chromedriver.exe")
chrome.get('https://www.handelsregister.de/')
assert "Register" in chrome.title
normal_search = chrome.find_element(by=By.ID, value='main.search')
normal_search.send_keys(Keys.ENTER)
# input_company_name_0 = chrome.find_element_by_id('container')
# input_company_name_1 = chrome.find_element_by_id('inhalt')
# input_company_name_2 = chrome.find_element_by_id('suchparameterForm')
input_company_name = chrome.find_element_by_name('schlagwoerter')
input_company_name.clear()
input_company_name.send_keys('Sparkasse Dortmund')
keywords = chrome.find_element_by_id('schlagwortOption3')
keywords.send_keys(Keys.SPACE)
find_button = chrome.find_element_by_id('submitBtn')
find_button.send_keys(Keys.ENTER)
choose_class_CD_0 = chrome.find_element(by=By.CLASS_NAME, value='RegPortErg')
# choose_class_CD_1 = chrome.find_element(by=By.CLASS_NAME, value='RegPortErg_RandRechts')
choose_CD = chrome.find_element(by=By.LINK_TEXT, value="CD")
choose_CD.send_keys(Keys.ENTER)