Python/Selenium - no such element

Asked

Viewed 1,122 times

-1

Hello, I’m developing a Python automation with the Selenium library to access the Econet website and retrieve data from a table from an NCM code. At first I wrote the code to open the browser, access the user and search part for NCM’s. But when clicking on the field to enter the NCM I find the following error:

Traceback (most recent call last):
  File "C:/Users/gusta/PyCharmProjects/WebScraping/tes.py", line 32, in <module>
    driver.find_element_by_xpath('/html/body/form/table/tbody/tr[1]/td[2]/input').click()   #click no campo para inserir o NCM
  File "C:\Users\gusta\PyCharmProjects\WebScraping\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Users\gusta\PyCharmProjects\WebScraping\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\gusta\PyCharmProjects\WebScraping\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\gusta\PyCharmProjects\WebScraping\venv\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":"xpath","selector":"/html/body/form/table/tbody/tr[1]/td[2]/input"}
  (Session info: chrome=85.0.4183.121)

Follow the code below:

import time
from selenium import webdriver
# --- #

# --- # chamando o chrome --- #
chromedriver = "C:/Users/gusta/Downloads/chromedriver"
driver = webdriver.Chrome(chromedriver)

url = 'http://www.econeteditora.com.br/'

driver.get(url)
time.sleep(0)

# --- #

login = "/html/body/div[2]/div[2]/div[1]/div/table/tbody/tr[2]/td[1]/div[1]/table/tbody/tr[2]/td[2]/form/table/tbody/tr[1]/td[2]/input"
senha = "/html/body/div[2]/div[2]/div[1]/div/table/tbody/tr[2]/td[1]/div[1]/table/tbody/tr[2]/td[2]/form/table/tbody/tr[2]/td[2]/input[1]"

# --- # login econet
driver.find_element_by_xpath(login).click()
driver.find_element_by_xpath(login).send_keys("")  #usuário
driver.find_element_by_xpath(senha).click()
driver.find_element_by_xpath(senha).send_keys("")  #senha
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/table/tbody/tr[2]/td[1]/div[1]/table/tbody/tr[2]/td[2]/form/table/tbody/tr[3]/td/img').click()
time.sleep(2)

# --- # busca ncm

driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/table/tbody/tr[2]/td[1]/div[3]/div[1]/ul/li[16]/a').click()     #click na barra de opções lado esquerdo (NCM)
time.sleep(2)

driver.find_element_by_xpath('/html/body/form/table/tbody/tr[1]/td[2]/input').click()   #click no campo para inserir o NCM

time.sleep(2)

# --- #
#driver.quit()
  • Gustavo, good morning! In your original code with 'user' and 'password' you can access the next page?

  • Imonferrari, good morning! I can do it. When he needs to click on line 32, he gives me the error.

1 answer

0

Since I don’t have access to the page, try this way:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

# --- # chamando o chrome --- #
chromedriver = "./chromedriver.exe"
driver = webdriver.Chrome(chromedriver)
url = 'http://www.econeteditora.com.br/'
driver.get(url)

# --- #
login = 'Log'
senha = 'Sen'
input_log = 'btn_logar'

# --- # login econet

driver.find_element_by_name(login).send_keys('teste')
driver.find_element_by_name(senha).send_keys('teste')
driver.find_element_by_id(input_log).click()

# --- # busca ncm
delay = 5
try:
    WebDriverWait(driver, delay).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div[2]/div[1]/div/table/tbody/tr[2]/td[1]/div[3]/div[1]/ul/li[16]/a'))).click()
    WebDriverWait(driver, delay).until(EC.element_to_be_clickable((By.XPATH, '/html/body/form/table/tbody/tr[1]/td[2]/input'))).click()
except TimeoutException:
    print('Tempo esgotado.')
# --- #
#driver.quit()

Added a delay time that can be changed in the delay variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.