My test script are developed using Java with Selenium webdriver api. There is 1 particular scenario where I need to click on a sub option loaded from a dropdown menu but I am not able to do that. Following are the test steps and the screenshot for the particular problem.
-Launch Microsoft Outlook Web App (OWA) and login
-On main screen I need to enter some text in Search Field
-Click the drop down next to it
-Select "This Folder" from the options loaded
(Screenshot)
I dont see any frameid so not using any. Dropdown works fine but failing to click on suboption.
Adding the code which I am using for click this
public static final By searchDropDown_locator= By.xpath(".//*[#id='divSScp']");
public static final By thisFolderText_locator= By.xpath("(.//*[#id='spnT' and text()='This Folder'])[2]");
public void clickSearchDropDown()
{
WebElement searchIcon= websitedriver.findElement(searchDropDown_locator);
searchIcon.click();
}
public void clickThisFolder()
{
WebElement searchIcon= websitedriver.findElement(thisFolderText_locator);
searchIcon.click();
}
I am calling both these functions in my script file.
What could be the solution here.
Try to use JavascriptExecutor for click as below
public void clickSearchDropDown()
{
WebElement searchIcon= websitedriver.findElement(searchDropDown_locator);
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", searchIcon);
}
public void clickThisFolder()
{
WebElement searchIcon= websitedriver.findElement(thisFolderText_locator);
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", searchIcon);
}
Hope it will help you :)
Related
Not selecting from city field by using selenium webdriver and java language on Makemytrip application.
public class LoginPage {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.makemytrip.com/");
driver.manage().window().maximize();
driver.findElement(By.id("fromCity")).click();
}
}
console error:
Exception in thread "main" org.openqa.selenium.WebDriverException:
element click intercepted: Element input data-cy="fromCity"
id="fromCity" type="text" class="fsw_inputField font30 lineHeight36
latoBlack" readonly="" value="Delhi"> is not clickable at point (244,
255). Other element would receive the click: div
data-cy="outsideModal" class="loginModal displayBlock modalLogin
dynHeight personal ">
You can use below code to click as the thing that you are trying can be done by first moving the mouse pointer to that element and then click on that element .
So use below code :
WebDriver driver = new ChromeDriver();
driver.get("http://www.makemytrip.com/");
driver.manage().window().maximize();
Actions action=new Actions(driver);
WebElement fromCity=driver.findElement(By.id("fromCity"));
action.moveToElement(fromCity).doubleClick().perform();
As per console error :element click intercepted.Another webelement with class that starts like loginmodal is expecting click.There is a login page or frame that is embedded onto main page.Hence element you are trying to click is hidden.
You can use the following to find no of frames
List frame=driver.findElements(By.tagname(‘iframe’));
System.out.println(“no of frames: ” + frame.size());
You can switch to frame with help of
driver.switchTo().frame(1);
After that handle login page and then switch to main page by
driver.switchTo().defaultContent();
This will redirect you to main page and then try locating web element you desire.
I need a little help. I'm trying to run an automated test on the website http://zara.com and i want to select the language from the language dropdown.
This is the HTML code from Zara. https://prntscr.com/g6hdiv
This is the code i've tried with Selenium 2.53 in IntelliJ
public class RegistrationTest {
WebDriver driver;
#Before
public void setUp(){
driver = new FirefoxDriver();
driver.get("http://zara.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#After
public void tearDown(){
driver.quit();
}
#Test
public void test(){
WebElement languageDropdown = driver.findElement(By.id("language"));
Select selectLanguage = new Select(languageDropdown);
selectLanguage.selectByValue("en");
}
}
I always receive the error below even if I've tried in different setups but it didn't work.
org.openqa.selenium.ElementNotVisibleException: The element is not currently visible and so may not be interacted with
Could you please tell me what am I doing wrong?
Appreciate the help.
The element is not currently visible and so may not be interacted with
You need to scroll the page, so that the element is in the current viewport. Something like this:
WebElement languageDropdown = driver.findElement(By.id("language"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", languageDropdown);
Select selectLanguage = new Select(languageDropdown);
selectLanguage.selectByValue("en");
I am Having a Lot of WebElements
For Example I Declared a WebElement a
#FindBy(id="BtnLogin")
private WebElement btnLogin;
In the Same Manner I created "N" number of WebElements
Every time I Cant use "driver.findElement()" function So I wrote a function
public static void WebElementClick(WebElement we)
{
we.click();
}
When Ever the Control is Going to The Line we.click() in the WebElementclick Function it is Showing NullPointerException as a Result My Purpose is Failing
I am Not Getting What to Do,Some One Please Help Me on this :)
Your WebElementClick should receive the selector and it should: find element -> click, you can get an example from the above link.
In your case you it seems that you are not using wait and the WebElementClick it tries to click on the string.
Using find will return an object that will make click available.
The method should contain something like: driver.findElement(By.xpath("your_selector"));
Ant then use click on what this method returns.You can use also css if you want to.
public class testJava{
#Test
public void testMethod() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
pageClass pageClass = PageFactory.initElements(driver, pageClass.class);
driver.get("http://www.facebook.com");
Thread.sleep(5000);
pageClass.clickLoginBtn();
}}
public class pageClass {
#FindBy(id = "loginbutton")
private WebElement loginBtn;
WebDriver driver;
public pageClass(WebDriver driver) {
this.driver = driver;
}
public void clickLoginBtn()
{
click(loginBtn);
}
public void click(WebElement we)
{
we.click();
}}
Its best practice to use the page class & test class..Try this it will help you i guess.
You are suppose to use driver to find & click the element.
I think that driver may try to click element before it's presented. Good practice before clicking WebElement is to wait for WebElement being clickable. I would try:
public static void WebElementClick(WebElement we)
{
wait.forElementClickable(we);
we.click();
}
I am learning Selenium Webdriver using Java.
As a learning example, I tried to open MakeMyTrip, access International Flights page and click on One Way radio button in Google Chrome.
I tried different ways to locate this radio button but it's still not working.
Please find below my code sample.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TryRadioClass {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "Chrome exe path");
WebDriver driver=new ChromeDriver();
driver.get("http://www.makemytrip.com/international-flights");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
boolean displayFlag = driver.findElement(By.linkText("ONE WAY")).isDisplayed();
System.out.println("Display Flag :- "+displayFlag);
boolean enableFlag = driver.findElement(By.linkText("ONE WAY")).isEnabled();
System.out.println("Enable Flag :- "+enableFlag);
if(displayFlag==true && enableFlag==true)
{
WebElement element=driver.findElement(By.linkText("ONE WAY"));
element.click();
System.out.println("Tried to click One Way");
}
}
}
Can anyone please help me to resolve this issue?
Use below code :-
if(displayFlag==true && enableFlag==true)
{
try{
Thread.sleep(5000);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
WebElement element=driver.findElement(By.xpath("//span[#class='radio_state']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
System.out.println("Tried to click One Way");
}
enjoy .. get back to me if still getting any issue :)
Clicking on link sometimes might skip checking the radio button. Try clicking on the radio button (or input html tag) directly rather than clicking on the anchor tag. Here's an example -
WebElement ele=driver.findElement(By.xpath("//input[#value='one_way']"));
ele.click();
Hope this helps.
Try to never use Thread.sleep(time)
Never use general Exception catching.
Instead of it try this, what is more safe:
public Boolean isDisplayed() {
try {
wait.withTimeout(20).until(new ExpectedCondition<Boolean>() {
#Nullable #Override public Boolean apply(WebDriver input) {
return videoComponent.isDisplayed();
}
});
} catch (TimeoutException e) {
return false;
}
return true;
}
This code will check markup within 20 seconds until it return true.If not, after 20 sec it will return false.
Always specify Exception type, which you want to catch. In other cases it is useless.
Try below xpath.
//*[#id="one_way_button1"]/span/input
It should work.
driver.findElement(By.xpath(//*[#id=\"one_way_button1\"]/span/input)).click();
As per my check, the specific radio button has following structure:
<a id="one_way_button1" href="javascript:void(0);" onclick="change_trip_type('one_way_button', 'trip_type', 'o');" class="one_way_button trip_type row first seg_text" tabindex="1">
<span class="radio_state">
<input type="radio" name="way_fields" value="one_way">
</span> ONE WAY
</a>
So what you are gonna to click is not the tag a which contains 'ONE WAY' but the span inside. You may have a try to locate the span by using xpath
"//a[text()='ONE WAY']/span[#class='radio_state']"
We have found in the past that with a radio and a click that the above happens (Sometimes it clicks it sometimes it does not) and as we try and steer clear of wait for tasks/Thread.Sleep (Due to then timing issues it then can cause on different environments). This alone can become a giant headache quick in using thread.sleeps :p
We have personally found that sometimes the best solution is to send a click then a (Sendkeys.Enter or a Sendkeys.Space) or just send the (Sendkeys.Enter or a Sendkeys.Space) only and don't use the click with a radio.
Especially on pages that use Telerik controls. This then tends to work on multiple machines/environments without the need to add a Thread.Sleep and make every test which uses that step take way longer than it needs to (Trust me when you have 1000's of tests that 5s thread.sleep soon adds up if a lot of tests use the same method)
Just throwing in another possible solution into the mix...
This is the more proper way to do this and I just tested it and it works fine. The best practice is to wait for the element to be clickable. You do that using WebDriverWait with ExpectedConditions. What this will do is wait up to 20s for the element to appear, when it does execution continues. This is a big advantage over Thread.sleep().
driver.get("http://www.makemytrip.com/international-flights");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement oneWayRadioButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("one_way_button1")));
oneWayRadioButton.click();
System.out.println("Clicked One Way");
I tried many different ways to force selenium to wait for the radio button to be visible but it kept timing out. I eventually had to settle for clicking the label for:
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = driver.findElement(selector);
wait.until(ExpectedConditions.elementToBeClickable(element));
element.click();
Where selected is defined by:
By selector = By.cssSelector(cssSelector);
With HTML looking like this:
...
<div class="multi-choice">
<input id="wibble" type="radio" name="wobble" value="TEXT">
<label for="wibble">Wobble</label>
</div>
...
And an example string cssSelector of:
String cssSelector = "label[for='wibble']"
I am trying to automate search box of Amazon.in and when i try to enter some string over there, it rather points towards the address bar of browser. My code for the same.
Note- I have already tried with different xpaths using firebug and also through tag traversing.
Also please let me know why we have to use always build and perform methods with actions?
public static void main(String args[])
{
WebDriver driver= new FirefoxDriver();
driver.get("http://amazon.in");
Actions action=new Actions(driver);
WebElement element= driver.findElement(By.xpath(".//*[#id='nav-link-yourAccount']/span[2]"));
action.moveToElement(element).build().perform();
WebElement search= driver.findElement(By.xpath(".//*[#id='twotabsearchtextbox']"));
action.keyDown(Keys.SHIFT).moveToElement(search).sendKeys("teststring").build().perform();
action.contextClick(search).build().perform();
}
public static void main(String args[]) {
WebDriver driver = new FirefoxDriver();
driver.get("http://amazon.in");
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
Actions action = new Actions(driver);
WebElement search = driver.findElement(By.xpath(".//*[#id='twotabsearchtextbox']"));
//Search using actions by combining entering search string and then hit enter
action.click(search).sendKeys("Test").sendKeys(Keys.RETURN).build().perform();
// This also works where it does the same without actions class
search.sendKeys("test");
search.sendKeys(Keys.RETURN);
}
In your code:
Below line enters teststring into the browser search instead of the amazon search bar because you are just moving to that element and not clicking on it. action.keyDown(Keys.SHIFT).moveToElement(search).sendKeys("teststring").build().perform();
This like right clicks / context click on the search bar
action.contextClick(search).build().perform();
From the API doc:
build() Generates a composite action containing all actions so far,
ready to be performed (and resets the internal builder state, so
subsequent calls to build() will contain fresh sequences).
perform() A convenience method for performing the actions without
calling build() first
Please read below links for a clear picture on them:
LinkOne
LinkTwo