Iam getting an unexpected error while using javascript | Selenium Forum
R
Rupa Posted on 13/07/2020
package DropDown;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class EntryCountry {
	
	static WebDriver driver;
	
	public static void main(String[] args) throws Exception {
		
	driver=new ChromeDriver();
	driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
	driver.manage().window().maximize();
	driver.get("http://way2automation.com/way2auto_jquery/index.php");
	driver.findElement(By.xpath("//div[@class='fancybox-inner']//a")).click();
	driver.findElement(By.xpath("//div[@id='login']//input[@name='username']")).sendKeys("Rupa Basuthkar");
	driver.findElement(By.xpath("//div[@id='login']//input[@name='password']")).sendKeys("govinda");
	driver.findElement(By.xpath("//div[@id='login']//input[@value='Submit']")).click();
	Thread.sleep(2000);
	driver.findElement(By.xpath("//div[@class='linkbox margin-bottom-20'][4]/ul/li[2]")).click();
	driver.findElement(By.xpath("//div[@class='internal_navi']/ul/li[2]")).click();
	WebElement select=driver.findElement(By.xpath("//iframe[@src='dropdown/default1.html']"));
	driver.switchTo().frame(select);
	
	driver.findElement(By.xpath("//a[@role='button']")).click();
	
	/*WebElement selectCountry=driver.findElement(By.xpath("//select[@id='combobox']"));
	
	driver.findElement(By.xpath("//a[@class='ui-button ui-widget ui-state-default ui-button-icon-only custom-combobox-toggle ui-corner-right']")).click();
	Select countrySelector=new Select(selectCountry);
	List<WebElement> listOfCountry=countrySelector.getOptions();
	
	System.out.println(listOfCountry.size());
	
	for (int i = 0; i < listOfCountry.size(); i++) {
	
		System.out.println(listOfCountry.get(i).getText());
	}
	*/
	
	String boxXpath="//ul[@id='ui-id-1']";
	
	WebElement boxList=driver.findElement(By.xpath(boxXpath));
	List<WebElement> listOfCountry=driver.findElements(By.tagName("li"));
	System.out.println(listOfCountry.size());
	
	for (int i = 0; i < listOfCountry.size(); i++) {
		
		System.out.println(listOfCountry.get(i).getText());
	}
	
	System.out.println("=============================");
	WebDriverWait wait=new WebDriverWait(driver, 20);
	wait.until(ExpectedConditions.visibilityOfAllElements(driver.findElement(By.xpath("//span[@class='custom-combobox']"))));
	driver.findElement(By.xpath("//span[@class='custom-combobox']")).click();
	System.out.println("=============================");
	driver.findElement(By.xpath("//input[@class='custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left ui-autocomplete-input']")).clear();
	System.out.println("=============================");
	driver.findElement(By.xpath("//input[@class='custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left ui-autocomplete-input']")).sendKeys("Ind");
	driver.findElement(By.xpath("//input[@class='custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left ui-autocomplete-input']")).sendKeys(Keys.DOWN);
	driver.findElement(By.xpath("//input[@class='custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left ui-autocomplete-input']")).sendKeys(Keys.DOWN);
	
	System.out.println(driver.findElement(By.xpath("//input[@class='custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left ui-autocomplete-input']")).getText());
	
	JavascriptExecutor js=(JavascriptExecutor)driver;
	
	String script="return.documentOfElementById('//input[@class='custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left ui-autocomplete-input']').value;";
	
	String text=(String) js.executeScript(script);
	
	System.out.println(text);
	
	
	}
}

A
Ashish Thakur Replied on 15/07/2020

You cannot use xpath in document.geteElementById

	String script="return.documentOfElementById('//input[@class='custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left ui-autocomplete-input']').value;";


Related Posts