T
T
TechNOIR2017-01-19 21:57:55
Python
TechNOIR, 2017-01-19 21:57:55

Selenium+Python. Doesn't find the element and exits the script. What is the reason?

Good afternoon. There is a script that takes a proxy from the proxylist, goes to the page and performs a click, and so on until all proxies have passed. There is a page timeout condition. But if suddenly the element is not found, then the script ends, and it is necessary to go to the next proxy in the proxy list until everything passes. Tell me how to do it? Another condition?

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
import time
with open('proxy.txt', 'r') as file:
    proxy_list = [a.strip() for a in file.readlines()]
for proxy in proxy_list:
    service_args = [
        '--proxy={}'.format(proxy),
        '--proxy-type=http',
        ]
    browser=webdriver.PhantomJS(service_args=service_args)
    browser.set_page_load_timeout(20)
    try:
        print("TRY PROXY")
        browser.get('http://nnmclub.to')
        current = browser.window_handles[0]
        print ("Page is loaded")
        print ("Click")     
        browser.find_element_by_xpath("//a[contains(@href,'marketgid')]").click()
        print ("Clicked")
        newWindow = [window for window in browser.window_handles if window != current][0]
        browser.switch_to.window(newWindow)
        print("Done.")
    except TimeoutException:
        print("cant load")
    browser.get_screenshot_as_file('po{}.png'.format(proxy))
browser.quit()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael, 2017-01-19
@Araben

from selenium.common.exceptions import NoSuchElementException
try:
    elem=driver.find_element_by_xpath("//a[contains(@href,'marketgid')]") 
except NoSuchElementException:
    print "Missing"
else:
    elem.click()
    и что там дальше

E
Emil Revencu, 2017-01-21
@Revencu

I noticed that LXML parsing is faster than Selenium.
And if the element is missing on the page, then Selenium also slows down a lot.
The solution could be:

...
from lxml import html
...
code=driver.page_source
tree=html.fromstring(code)
el=tree.xpath("//a[contains(@href,'marketgid')]")
if len(el)>0:
    elem=driver.find_element_by_xpath("//a[contains(@href,'marketgid')]").click()
else:
    ....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question