· 7 years ago · Sep 09, 2018, 07:10 PM
1from selenium import webdriver
2from selenium.common.exceptions import NoSuchElementException
3from selenium.webdriver.common.by import By
4from selenium.webdriver.support import expected_conditions as EC
5from selenium.webdriver.support.ui import WebDriverWait
6from urllib3.exceptions import MaxRetryError
7
8if __name__ == '__main__':
9 driver = None
10 ff_opts = webdriver.FirefoxOptions()
11 # TODO: headless work with errors - I'm comment him
12 # ff_opts.set_headless()
13
14 try:
15 # initialize Webdriver FF
16 driver = webdriver.Firefox(options=ff_opts, log_path='/dev/null')
17 driver.implicitly_wait(10)
18 driver.get('https://yandex.ru')
19
20 # find input field on yandex.ru
21 search_line = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "input__input")))
22
23 # find submit button on yandex.ru
24 search_line.send_keys('povercon.ru')
25 submit = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "suggest2-form__button")))
26 submit.click()
27
28 # click by first button with povercon.ru link on TOP10 SERP
29 site_link = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.LINK_TEXT, "povercon.ru")))
30 site_link.click()
31
32
33 except MaxRetryError as e:
34 print('retrieved connection - is damn sucks!')
35
36 except NoSuchElementException as e:
37 print('Somenthing went wrong... Not find an element of the DOM')
38
39 finally:
40 # driver.quit()
41 pass