0

I'm creating a project using web scraping , I'm having trouble extracting information from an Iframe form, when I try to extract the values ​​of the name , position and company field.

Code I'm testing:

replay = browser.switch_to.frame(browser.find_element(By.XPATH, "/html/body/div[1]/text()[1]")).get_text().strip()

it is giving the following error:

"selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "/html/body/div[1]/text()[1]" is: [object Text]. It should be an element .

I send an image of the form and the information I'm trying to get as an attachment, can anyone give me some tips?enter image description here

<iframe id="content210835787_ifr" frameborder="0" allowtransparency="true" title="Área de texto formatado.Pressione ALT-F9 para exibir o menu, ALT-F10 para exibir a barra de ferramentas ou ALT-0 para exibir a ajuda" style="width: 100%; height: 465px; display: block;" data-mce-style="width: 100%; height: 465px; display: block;"class="selectorgadget_selected"></iframe>



<body id="tinymce" class="mce-content-body " data-id="content210835787" contenteditable="true"style="overflow-y: hidden; padding-left: 1px; padding-right: 1px; padding-bottom: 50px;" data-mce-style="overflow-y: hidden; padding-left: 1px; padding-right: 1px; padding-bottom: 50px;"><h2><strong>Formulário - Confecção de usuário de acesso</strong></h2><div>Nome Completo: &nbsp; Solicitação aberta para teste<br><br>Matrícula:&nbsp; 2354<br><br>Centro de Custo:&nbsp; VS | 123 </div><div>&nbsp; <br><br>Cargo: &nbsp; Analista de Teste</div><div><br></div><div><br><br>&nbsp; <br><br>&nbsp; <br><br>Tipo de Acesso: &nbsp; Rede<br><br>Empresa que o colaborador foi cadastrado pelo RH? &nbsp; VS EMpresarial</div></body>

The yellow markings are the information I'm trying to get

vander
  • 3
  • 2
  • Are you sure it's in the iframe. – Arundeep Chohan Jul 15 '22 at 23:41
  • Arundeep Chohan, Maybe that's where I'm sinning, my code is with the URL, after authenticating on the site, and then I send this command: replay = browser.switch_to.frame(browser.find_element(By.XPATH,"/html/body/div[1]")).get_text().strip() I'm starting to study web scripting, this code above I thought I was already accessing the Iframe. Do you have any tips for me to validate if I'm accessing the iframe? I've been trying for 5 days and I couldn't evolve – vander Jul 16 '22 at 03:54
  • Do you have the url I could write you a simple xpath for it or the html code. Cause what you have here isn't an xpath inside an element. – Arundeep Chohan Jul 16 '22 at 04:14

2 Answers2

0

Try following:

replay = browser.switch_to.frame(browser.find_element(By.XPATH,"/html/body/div[1]")).get_text().strip()

You need to choose just element by XPath and get the text by get_text() function.

Lukas Tomek
  • 96
  • 12
  • Lucas, I changed the code to what you suggested and gave the following error: "selenium.common.exceptions.NoSuchFrameException: Message: no such frame: element is not a frame" – vander Jul 15 '22 at 21:36
0

This could be optional:

browser.switch_to.frame(By.ID,"content210835787_ifr")

Mandatory

elem=browser.find_element(By.XPATH,"//body[@id='tinymce']/div")
print(elem.text)

Without a url to verify off I'm not too sure but switch to your iframe and then look for the body with that id and that div. Then print it's .text or .get_attribute('innerHTML')

Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
  • Arundeep Chohan , I was able to extract the information from the site using your tips. but the generated information is coming out like this: Nome Completo: solicitação aberta para teste Matrícula: 2354 Centro de Custo: VS | 123 Cargo: Analista de Teste Tipo de Acesso: Rede Empresa que o colaborador foi cadastrado pelo RH? VS EMpresarial – vander Jul 17 '22 at 16:57
  • Is there a way to assign these text to a variable? Example: Variables: Primeiro_Nome = solicitação Segundo_Nome = aberta para teste matricula = 2354 CCustro = VS | 123 cargo = Analista de teste tipo de acesso= Rede Empresa: VS EMpresarial – vander Jul 17 '22 at 16:58
  • You can selectively grab them by finding them by their selector. – Arundeep Chohan Jul 17 '22 at 17:11