-2
Hello, I am creating an automation with the webdriver and panda in python, where the values of the cells of a certain excel spreadsheet (xlsx) should be typed in the Chrome page. However, in a certain field of the page when I try to use send Keys, it just doesn’t type. This is a financial value field, which by default has the value "0.00" displayed.
Below the code, where the error is in the find element of the value :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.support.ui import Select
from selenium.webdriver import ActionChains
import os
import pandas as pd
teste= pd.read_excel(r'C:\Users\Documentos\teste.xlsx')
navegador = webdriver.Chrome()
action = ActionChains(navegador)
login="xxx"
senha="xxxx"
navegador.get("site")
navegador.maximize_window()
navegador.find_element_by_xpath('//*[@id="Usr"]').send_keys(login)
navegador.find_element_by_xpath('//*[@id="Pwd"]').send_keys(senha)
navegador.find_element_by_xpath('//*[@id="btnSubmit"]').click()
time.sleep(1)
navegador.find_element_by_xpath('//*[@id="menu-bar"]/a[2]/img').click()
ddelement= Select(navegador.find_element_by_xpath('//*[@id="_tabSup"]/tbody/tr/td[1]/select[1]'))
ddelement.select_by_index(34)
time.sleep(4)
navegador.find_element_by_xpath('//*[@id="_divMnu"]/table[1]/tbody/tr[2]/td/a[2]').click()
local= navegador.find_element_by_xpath('//*[@id="header_txt2"]')
action.context_click(local).perform()
navegador.find_element_by_xpath('//*[@id="gridLv_menu_itemMenu_0"]').click()
for column in teste.index:
parte=teste.loc[column,'PARTE']
time.sleep(1)
navegador.find_element_by_xpath(' //*[@id="genSearch_is_cpSearch"]/input').send_keys(parte)
navegador.find_element_by_xpath('/html/body/center/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[3]/td[2]/button ').click()
time.sleep(2)
navegador.switch_to.frame(navegador.find_element_by_tag_name("iframe"))
valor = teste.loc[column,'VALOR']
navegador.find_element_by_xpath("//*[@id=el__2019434]/input").send_keys(valor)
The print of the field
The print of the HTML structure
I have tried all possibilities but in none of them the field is filled, including with css selector.
Thank you!
this in a td, this class "Field" has only one ?... in the value there in the image.. the tag class="Field" has only in this field ?
– stack.cardoso
The "Field" class repeats 4x in the entire html, but in desssa td has only in this field.
– João Martins
tries to make a filter in this td, to take this field, in Selenium da to take a part of the html I think so it is easier to catch the class inside the td.
– stack.cardoso
I tried filtering using the.find_elements_by_class_name driver but I was still unsuccessful. Vc suggests some other type of filter?
– João Martins
this id in td is generated a new one after a few minutes, looking again that the xpath to a decimal id different from the image.. take a look at the id of td if it is generated a new one every page load.
– stack.cardoso
I just confirmed that the id changes every time the page is reloaded, apparently without a pattern. Is there any way to anticipate the id number or some other way to get the xpath?
– João Martins
I’ve been there, scrap the td by taking the id and the poem sequence in xpath.. so "//*[@id={id_pegado}]/input" using f string
– stack.cardoso
find_element.. catching td and the . get_attribute("outerHTML") function captures the region.. ai only do a regular expression search to get the id sequence and this id only play in the browser.find_element_by_xpath("//*[@id="{id_captured}]/input"). send_keys(value)
– stack.cardoso
I never used the . get_attribute("outerHTML") function, but if I understood the result of this function I will put inside the id of xpath p finally use Sendkeys? this way, future id sequences will already be correct?
– João Martins
this function does the following. takes a field for example the <td> ... <td/> everything that is inside this tag.. ai just take the return that will be the html code and make a grep to capture the "id" and play inside the xpath... browser.find_element_by_xpath("//*[@id={aquiOID}]/input"). send_keys(value)
– stack.cardoso
I tried using it as follows: codig = browser.find_element_by_xpath('//*[@id="el__43008"]') codig2=codig.get_attribute("outerHTML") but when I put print(codig2), it returns me: <input internalname="idValor" class="Field" style="text-align: right;">. I’m not understanding how to get the id to include inside the xpath
– João Martins