0

I had to make chrome browser headless after which when I try to see the html code by executing below lines

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
ldriver = new ChromeDriver(options);
String stored_Text = ldriver.getPageSource();
System.out.println(stored_Text);

Output is:

<html><head></head><body></body></html>

Because of which when I write xpath after making the page headless I'm getting no such element exception

Is there a way for me to circumvent this problem. Please help

Sreedhar Danturthi
  • 7,119
  • 19
  • 68
  • 111

1 Answers1

0

Before invoking getPageSource() ideally you need to induce WebDriverWait for the visibilityOfElementLocated() for any of the static and visible element on the page.


An example

As an example to extract the page_source of Google Home Page you need to wait for the Search Box to be visible as follows:

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
ldriver = new ChromeDriver(options);
ldriver.get("https://www.google.com/");
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.name("q")));
String stored_Text = ldriver.getPageSource();
System.out.println(stored_Text);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352