1

How do I select/copy info from web with Selenium and copy into IntelliJ to String? I'm using IntelliJ in Java. I am trying to use:

Navegador.getInstance().instanciaNavegador().findElement(By.xpath("//div[@id='cpf']/span[2]")).sendKeys(Keys.chord(Keys.CONTROL, "v"));
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

I am not sure what Navegador does and it really does not matter. The key here are the Selenium methods you are invoking. For example, to write to a web element, sendKeys() is appropriate. But, based on the description of your post, you need to read from the page and not write to it. One way to obtain the text from a page is

WebElement myElement = driver.findElement(By.xpath("...")); // Provide the XPath expression for the element containing the text you need.
String myText = myElement.getText();  // Saves the obtained text into a String object

Outside of this, I am not sure what else you need. Again, based on the limited information here, this seems all you need.

hfontanez
  • 5,774
  • 2
  • 25
  • 37
0

To select and copy any info from any webpage using Selenium and send it to the IntelliJ console you can use the following solution:

//required imports
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

// simulating CONTROL + A
driver.findElement(By.foo("bar")).sendKeys(Keys.chord(Keys.CONTROL, "a"));
// simulating CONTROL + C
driver.findElement(By.foo("bar")).sendKeys(Keys.chord(Keys.CONTROL, "c"));
// extracting the text from the clipboard
String myText = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor); 
// printing the text
System.out.println(myText)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352