E
E
Ernest Vardanyan2020-05-10 18:15:02
Python
Ernest Vardanyan, 2020-05-10 18:15:02

Why is NoSuchElementException not working?

I need to parse rounds, there are only 9 of them and for example 3 may or may not appear when I am already parsing the battle, and when parsing 1 and 2 rounds, going to 3 it writes an error, I would like the exception to be simply ignored and moved on, that is, to the next battle, the variables fd and fe, there are just 1 and 2 rounds, there may be, as I already said 9, but I didn’t add it, I just want to understand the essence, here is the code:

from selenium import webdriver
import time
from selenium.common.exceptions import NoSuchElementException

browser=webdriver.Firefox()
browser.maximize_window()

print('ИДЁТ ПАРСИНГ ПЕРВОГО БОЯ.........')

browser.get('https://one-xskbdc.world/ru/live/Mortal-Kombat/1252965-Mortal-Kombat-X/')


nameone1=browser.find_element_by_xpath('//*[@id="games_content"]/div/div[1]/div/div/div[2]/div/div[1]/div[1]/a/span/div[1]').text
print(nameone1)
nametwo1=browser.find_element_by_xpath('//*[@id="games_content"]/div/div[1]/div/div/div[2]/div/div[1]/div[1]/a/span/div[2]').text
print(nametwo1)


browser.find_element_by_xpath('//*[@id="games_content"]/div/div[1]/div/div/div[2]/div/div[1]/div[1]/a/span/div[2]').click()
time.sleep(3)
browser.find_element_by_xpath('//*[@id="hottest_games"]/div/div[1]/div/div/div[2]/div/ul/li[1]/a').click()
time.sleep(3)

score1=browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div/div/div[2]/div/div/div/div[1]/div[1]/div/div[2]/div/div/div[2]/div/div/div[2]/div[1]').text
print(score1)
score1_2=browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div/div/div[2]/div/div/div/div[1]/div[1]/div/div[2]/div/div/div[2]/div/div/div[2]/div[2]').text
print(score1_2)

print('ПАРСИМ ТАБЛО.......')

fd=browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div/div/div[2]/div/div/div/div[1]/div[1]/div/div[2]/div/div/div[3]/div/section/div[1]/div[1]').text
print(fd)
fe=browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div/div/div[2]/div/div/div/div[1]/div[1]/div/div[2]/div/div/div[3]/div/section/div[1]/div[2]').text
print(fe)


try:
    fe = browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div/div/div[2]/div/div/div/div[1]/div[1]/div/div[2]/div/div/div[3]/div/section/div[1]/div[2]').text
    print(fe)
except NoSuchElementException:
    print('Поймали исключение')
    pass

try:
    fd = browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div/div/div[2]/div/div/div/div[1]/div[1]/div/div[2]/div/div/div[3]/div/section/div[1]/div[1]').text
    print(fd)
except NoSuchElementException:
    print('Поймали исключение')
    pass

browser.close()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question