I am using Chrome v81 and ChromeDriver vChromeDriver 81.0.4044.20. My selenium script is running successfully and able to identify web elements also. But i am using one loop there, which is not working-
public static void main(String[] args) throws InterruptedException {
String projectPath = System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver", projectPath + "\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://rahulshettyacademy.com/AutomationPractice/");
Thread.sleep(3000);
WebElement cb = driver.findElement(By.xpath("//input[#value='option1']"));
cb.click();
Thread.sleep(2000);
if (cb.isSelected())
cb.click();
Thread.sleep(3000);
List<WebElement> allCBs = driver.findElements(By.xpath("//input[#value='checkbox']"));
for (int i = 0; i < allCBs.size(); i++) {
System.out.println(i);
}
driver.close();
}
Output-
INFO: Detected dialect: W3C
[1588922113.669][SEVERE]: Timed out receiving message from renderer: 0.100
[1588922113.771][SEVERE]: Timed out receiving message from renderer: 0.100
Change your xpath with this:
//*[#id='checkbox-example']/child::*/descendant::input
Related
I'm trying to write test cases using selenium. I wrote this code. And i got this error
org.openqa.selenium.WebDriverException: Unable to parse remote response:
but I don't understand what is wrong
public static WebDriver driver=null;
ATUTestRecorder recorder;
#Parameters({"browsername"})
#BeforeTest
public void setUpBrowser(String browsername) throws ATUTestRecorderException, MalformedURLException
{
String timeStamp = new SimpleDateFormat("yy-MM-dd_HH-mm-ss").format(new Date());
recorder = new ATUTestRecorder("./ScreenRecorder/","Test_"+timeStamp+".mp4",false);
DesiredCapabilities cap =null;
if(driver==null)
{
if(browsername.equalsIgnoreCase("Firefox"))
{
// Set the path for geckodriver.exe
//System.setProperty("webdriver.gecko.driver", "./Drivers/geckodriver.exe");
//WebDriverManager.firefoxdriver().setup();
cap = DesiredCapabilities.firefox();
cap.setBrowserName("firefox");
cap.setPlatform(Platform.ANY);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),cap);
}
// If the browser is Chrome
else if(browsername.equalsIgnoreCase("Chrome"))
{
// Set the path for chromedriver.exe
System.setProperty("webdriver.chrome.driver", "./Drivers/chromedriver.exe");
//WebDriverManager.chromedriver().setup();
cap = DesiredCapabilities.chrome();
cap.setBrowserName("chrome");
cap.setPlatform(Platform.ANY);
System.out.println(driver);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),cap);
}
// If the browser is IE
else if(browsername.equalsIgnoreCase("IE"))
{
// Set the path for IEdriver.exe
//System.setProperty("webdriver.ie.driver", "./Drivers/IEDriverServer.exe");
//WebDriverManager.iedriver().setup();
cap = DesiredCapabilities.internetExplorer();
cap.setBrowserName("internetExplorer");
cap.setPlatform(Platform.ANY);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),cap);
}
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(DataConfig.baseURL);
}
recorder.start();
}
How can I resolve this issue?
Remove /wd/hub from your driver URL.
See example at https://www.selenium.dev/documentation/webdriver/remote_webdriver/
I have run my test script but I got this error message in the console tab:
"Starting ChromeDriver 84.0.4147.30 (48b3e868b4cc0aa7e8149519690b6f6949e110a8-refs/branch-heads/4147#{#310}) on port 30846
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Aug 22, 2020 5:41:42 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C"
Below is my java code:
package seleniumProjectTutorial;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumWebDriver {
WebDriver driver;
public void invokeBrowser() {
try {
System.setProperty("webdriver.chrome.driver","D:\\Selenium\\Drivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.get("https://www.google.com");
driver.close();
driver.quit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
SeleniumWebDriver myobject = new SeleniumWebDriver();
myobject.invokeBrowser();
}
}
Is there a need for me to fix it and how? Thank you.
If you wanted to stop this warning message in the console you need to pass --silent argument to chromedriver to stop console message. you can achieve this using 'withSilent(true)' method
Launch chromedriver using chromedriverservice as shown in below sample code
ChromeDriverService cdservice=new ChromeDriverService.Builder().usingDriverExecutable(new File("/path/to/chromedriver.exe"))
.withLogFile(new File("/path/to/chromedriver.log"))
.withSilent(true)
.usingAnyFreePort()
.build();
WebDriver driver = new ChromeDriver(cdservice);
driver.get("http://www.google.com");
I am not able to launch two apps in single script using APPIUM, it shows error as
An unknown server-side error occurred while processing the command.
The desired should not include both of an 'appPackage' and a
'browserName'
public class SalesLeadProg {
public AndroidDriver<MobileElement> driver;
private WebElement element; public WebDriverWait wait;
#BeforeMethod
public void setup() throws MalformedURLException, InterruptedException {
DOMConfigurator.configure("log4j.xml");
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("automationName", "UiAutomator2");
caps.setCapability("deviceName", "sunil");
caps.setCapability("udid", "422b21e7");
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "8.1.0");
caps.setCapability("appPackage", "com.mfcwl.mfc_dealer");
caps.setCapability("appActivity", "com.mfcwl.mfc_dealer.Activity.SplashActivity");
caps.setCapability("autoAcceptAlerts", true);
caps.setCapability("noReset", true);
caps.setCapability("browserName", "Chrome");
//it will launch the chrome browser in mobile
caps.setCapability(ChromeOptions.CAPABILITY, true);
ChromeOptions options=new ChromeOptions();
options.setExperimentalOption("androidPackage", "com.android.chrome");
caps.setCapability("autoGrantPermissions", true);
// to allow the permission of contacts camara, which comes in the begining when we install the app*/
caps.setCapability("unicodeKeyboard", true);
// to make the keyboard working
caps.setCapability("resetKeyboard", false);
// it
try {
driver = new AndroidDriver<MobileElement> (new URL("0.0.0.0:4723/wd/hub"),caps);
}
catch(MalformedURLException e) {
System.out.println(e);
}
driver.manage().timeouts().implicitlyWait(16, TimeUnit.SECONDS);
}
#Test
public void Dashboard() throws MalformedURLException, InterruptedException {
MobileElement email = driver.findElement(By.id("com.mfcwl.mfc_dealer:id/email"));
Thread.sleep(4000);
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(email));
boolean isElementPresent = email.isDisplayed();
System.out.println(isElementPresent);
Thread.sleep(4000);
driver.findElementById("com.mfcwl.mfc_dealer:id/email").clear();
driver.findElementById("com.mfcwl.mfc_dealer:id/email").sendKeys("prestige");
System.out.println("Entered ID");
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/password")).clear();
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/password")).sendKeys("Mahindra");
System.out.println("Entered Password");
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/email_sign_in_button")).click();
//to login
System.out.println("Signed IN");
Thread.sleep(4000);
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/homeTab")).click();
//to click on home button
System.out.println("Clicked on Home button");
Thread.sleep(3000); //Set ChromeDriver location
System.setProperty("webdriver.chrome.driver", "./ChromeDriver/chromedriver.exe");
driver.get("google.com/"); driver.findElement(By.id("com.mfcwl.mfc_dealer:id/click_lead")).click();
System.out.println("Clicked on Today's follow up leads section");
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/lead_filter")).click();
Please assist with a solution on how to use two apps in APPIUM with a single script.
I created a test using JUnit and in class #AfterClass I put the driver.quit () command to shut down the browser when the tests are finished but the eclipse displays the java.lang.NullPointerException message.
The test class populates several fields and then makes a query in the base, displays the result in the Eclipse console and should close the browser but displays the java.lang.NullPointerException message.
Below is the log and test script.
public class validarStatus {
private static WebDriver driver;
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver.exe");
}
#Test
public void validarStatusOs() throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("http://10.5.9.45/BKOMais_S86825EstrategiaBackOfficeClaroFixo");
driver.manage().window().maximize();
// Logar BkoMais
driver.findElement(By.id("matricula_I")).sendKeys("844502");
driver.findElement(By.id("senha_I")).sendKeys("Pw34Jdt#*");
driver.findElement(By.id("bt_entrar")).click();
// Logar na Estratégia
driver.findElement(By.id("mn_backoffice")).click();
driver.findElement(By.id("mn_bkoffice_prod_203")).click();// Produto
driver.findElement(By.id("mn_bkoffice_est_57")).click();// Estratégia
// Selecionado a atividade
Select atividade = new Select(driver.findElement(By.id("cboAtividade")));
atividade.selectByIndex(3);
// Registro >> Novo
Thread.sleep(500);
driver.findElement(By.id("mn_registro")).click();
driver.findElement(By.id("mn_novo_caso")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// Cod Os Estratégia VREL
String CodOs = driver.findElement(By.xpath("//*[#id=\"content\"]/div[1]/fieldset[1]/div[2]/div[3]/span"))
.getText();
// Campo Análise de Contrato
Select analiseContrato = new Select(driver.findElement(By.id("cboMotivo")));
analiseContrato.selectByIndex(5);
try {
// Campo Ação
Select acao = new Select(driver.findElement(By.id("cboSubMotivo")));
acao.selectByIndex(3);
// Status
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement ele = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("cboStatus")));
String valorStatus = ele.getText();
// driver.findElement(By.id("cboStatus")).getText();
Assert.assertEquals(" R", valorStatus);
// Chamado
driver.findElement(By.id("txtChamado")).sendKeys("Teste");
// Observação
driver.findElement(By.id("txtObservacao")).sendKeys("Teste 07/06/2018");
// Botão Salvar
driver.findElement(By.id("btnSalvar")).click();
} catch (StaleElementReferenceException e) {
// Campo Ação
Select acao = new Select(driver.findElement(By.id("cboSubMotivo")));
acao.selectByIndex(3);
// Status
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement ele = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("cboStatus")));
String valorStatus = ele.getText();
// String valorStatus = driver.findElement(By.id("cboStatus")).getText();
Assert.assertEquals(" R", valorStatus);
// Chamado
driver.findElement(By.id("txtChamado")).sendKeys("Teste");
// Observação
driver.findElement(By.id("txtObservacao")).sendKeys("Teste 07/06/2018");
// Botão Salvar
driver.findElement(By.id("btnSalvar")).click();
} catch (Exception e) {
// Campo Ação
Select acao = new Select(driver.findElement(By.id("cboSubMotivo")));
acao.selectByIndex(3);
// Status
String valorStatus = driver.findElement(By.id("cboStatus")).getText();
Assert.assertEquals(" R", valorStatus);
// Chamado
driver.findElement(By.id("txtChamado")).sendKeys("Teste");
// Observação
driver.findElement(By.id("txtObservacao")).sendKeys("Teste 07/06/2018");
// Botão Salvar
driver.findElement(By.id("btnSalvar")).click();
}
// Select na base para validar o status da NU_OS
ValidarEstrategiaPage p = new ValidarEstrategiaPage();
p.returnNuOs(CodOs);
// Saindo do Bko+
Thread.sleep(1000);
driver.findElement(By.linkText("Sair")).click();
}
#AfterClass
public static void closeBrowser() {
driver.quit();
}}
You clearly defined your WebDriver object as local variable inside the method:
#Test
public void validarStatusOs() throws InterruptedException {
WebDriver driver = new ChromeDriver();
In order for both the 'After' and 'Test' methods to interact with the global variable, change to:
#Test
public void validarStatusOs() throws InterruptedException {
driver = new ChromeDriver();
And BTW, change your class name from 'validarStatus' to 'ValidarStatus'. Starting a class name in upper case letter is a major best practice in Java.
As per your code block you have defined a global instance of WebDriver as:
private static WebDriver driver;
Within validarStatusOs() method you have initialized another local instance of WebDriver as:
WebDriver driver = new ChromeDriver();
When the control of your program comes out of validarStatusOs() method, the local instance of WebDriver is no more accessible.
So, when the control of your program enters within closeBrowser() method it tries to use the global instance of WebDriver and throws java.lang.NullPointerException.
Solution
As you have declared a global instance of WebDriver, use the same instance throughout your program. So you need to change the line:
WebDriver driver = new ChromeDriver();
To:
driver = new ChromeDriver();
i am getting UnhandledBrowserException when trying the below code in chrome:
public class myClass {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver", "C://Program Files (x86)//Google//Chrome//Application//chrome.exe");
WebDriver driver= new ChromeDriver();
String baseURL = "http://newtours.demoaut.com";
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = "";
// launch Firefox and direct it to the Base URL
driver.get(baseURL);
// get the actual value of the title
actualTitle = driver.getTitle();
/*
* compare the actual title of the page witht the expected one and print
* the result as "Passed" or "Failed"
*/
if (actualTitle.contentEquals(expectedTitle)){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
//close Firefox
driver.close();
// exit the program explicitly
System.exit(0);
}
It is launching a new session in chrome but then throwing the exception. Any help would be highly appreciated.
I think you are hitting program executable instead of driver.
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
It should be path to chrome driver not the chrome browser executable.
You can take a look at https://sites.google.com/a/chromium.org/chromedriver/getting-started