Monday, April 23, 2018

How to click on hidden element with Selenium WebDriver ?

How to click on hidden element with Selenium WebDriver ?

Solution :

It is very hot question asked in the Automation Interview.

Here, I will show you how you can click on Hidden Element with Selenium & Javascript.

For interacting with Hidden Elements we need to use "JavascriptExecutor" Interface.


Now, I need to perform click operation on the button which is disabled in DOM by using style=”display: none;”

Lets first create the instance of WebDriver :

WebDriver driver;
System.setProperty("webdriver.chrome.driver", "C:/Users/User/Downloads/chromedriver/chromedriver.exe");
driver = new ChromeDriver();
Next, we will open the url and find the WebElement i.e Button.

driver.get("file:///C:/Users/User/Desktop/Blog/Hidden.html");
WebElement btn = driver.findElement(By.cssSelector("button[id='myButton']"));
Here, we will create the object of "JavascriptExecutor" Interface and cast our WebDriver to "JavascriptExecutor".

After that we will use executeScript Method of JavascriptExecutor which will perform our task i.e "Click on hidden button".

JavascriptExecutor jsExecutor = (JavascriptExecutor)driver;
jsExecutor.executeScript("arguments[0].click();", btn);
Hope this will help you. Please share your views in Comment section below.

Thanks a lot.

Sunday, April 22, 2018

How to get the Integer or Numeric part of String in Java ?

How to get the Integer or Numeric part of String in Java ?

Solution :

String str = "| ABC CAR | VIX | 2017 | ABS";

System.out.println(str.replaceAll("[^0-9]", ""));

Output-
2017

Here we have replaced all the characters or whitespaces with blank - ("") .

Note :-

1. Replaceall()

Description
This method replaces each substring of this string that matches the given regular expression with the given replacement.
Signature:
public String replaceAll(String regex, String replacement) 
Reference : https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

2. Regular Expression - Character Classes

[^0-9] - This means any character except 0 to 9 will be replaced (Negation).

Reference : https://docs.oracle.com/javase/tutorial/essential/regex/char_classes.html