How to click Image icon using selenium webdriver - selenium

I am trying to click the image icon via xPath but when i run the code the link present on image icon is not opening. could you please help me in resolving this issue.
The Code i used to click the mail icon :
driver.findElement(By.xpath("//*[#id='e-switcher-mail-icon']")).click();

As slanec said more information is required or might be the element is not loaded. If u feel that the element has loaded and still its not happening, using java script is one way to click the image element.
Something like this
WebElement element = driver.findElement (By.xpath ("//*[#id='e-switcher-mail-icon']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript ("arguments[0].click();" , element);

Could be that the element hasn't yet loaded in the DOM. Try waiting for expected conditions:
Wait<WebDriver> wait= new FluentWait<WebDriver>(driver).withTimeout(15L, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS);
WebElement icon = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='e-switcher-mail-icon']")));
icon.click();

Touché. It's the frames. Either <frame> or <iframe>, both need special care, see the documentation on the topic.
What you need to do:
driver.switchTo().frame("s_MainFrame");
after this, the driver's context will switch to the frame and all searches will be done in it, so you should be able to find the element without any further problems.
Once you're done in the frame and you need to switch back to the default context of the page, do:
driver.switchTo().defaultContent();

Related

Unable to click submit button in selenium

I am working in a travel application. Once i filled the details like origin, destination i have to click the submit button. I tried with the xpath to click the submit button. But, i am unable to submit. Application is loading internally,once it clicks the xpath of submit. Please refer screen shot.
Selenium code: java driver.findElement(By.xpath(".//[#id='ctl00_cphMain_TravelRequest1_btnSubmit']")).click();
Please check image
Submitbutton- xpath code
can you please provide your html code for this issue,
there must be element present in that code,
you should have to use submit() instead of clicking on submit element.
actually click() works only on submit button of (you have to check this condition) and submit() works for all form elements and performs Enter key pressed like operations.
i hope this have to work for you,
driver.findElement(By.xpath(".//[#id='ctl00_cphMain_TravelRequest1_btnSubmit']")).submit();
or try using this,
driver.findElement(By.xpath(".//[#id='ctl00_cphMain_TravelRequest1_tpTravel_travel_gvTravel_ctl02_txtDuration']")).submit();
according to your html code we get element by id,
driver.findElement(By.Id("ctl00_cphMain_TravelRequest1_btnSubmit")).submit();
In your case, might be the submit button is visible but not clickable as you mentioned in the description. For that, you will have to wait until the element is not to be clickable using the ExplicitWait method. Perform click action after the Submit button gets clickable:
Hope below code will solve your problem.
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//[#id='ctl00_cphMain_TravelRequest1_btnSubmit']"))).click();
Sometimes the click() method does not work. Please try with below code once and let me know.
WebElement Login=driver.findElement(By.id("ctl00_cphMain_TravelRequest1_btnSubmit"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", Login);

Selenium Webdriver with Java: Not able to locate element on the browser popup window

Go to this URL: https://sports.ladbrokes.com/en-gb/
Click on "JOIN NOW" at the top right of the window
It opens a window pop up, where I need to enter 'First name' and other info
Though the 'First name' input box has the input tag with name attribute in the DOM, I am not able to find it with usual way. Please suggest!
When I say usual way, something straight forward like below is not working:
driver.findElement(By.xpath("//input[#name='firstname']"));
driver.findElement(By.xpath("//input[#placeholder='First name']"))
It's an iframe which means you have to switch to the iframe first before you can locate elements there.
this is untested but something similar to this
driver.switchTo().frame("lightbox-registration-frame")
driver.findElement(By.xpath("//input[#name='firstname']"));
driver.findElement(By.xpath("//input[#placeholder='First name']"))
driver.switchTo().defaultContent(); // <- important if need to switch back to original window
You may also need to use a wait to allow the iframe to appear/load. I noticed it took a second or 2 to load so if you try to switch immediately you'll end up with an unable to find iframe exception.
Also note that in my example i used "lightbox-registration-frame" to find the iframe which may not necessarily be correct. Generally you want to use the name or id of the iframe which i didn't see so you may need to use an index or some other method to find the iframe.
First you have to enter into the iframe and then locate the fields. try below code it works for you:
WebDriver driver= new FirefoxDriver();
driver.get("https://sports.ladbrokes.com/en-gb/");
// click on the Join now button
driver.findElement(By.xpath(".//[#id='loginSubmit']/div[3]/button[2]")).click();
//find Locator of frame
WebElement frampath= driver.findElement(By.xpath(".//iframe[(#class='lightbox-registration-frame')]"));
//switch to frame for operations
driver.switchTo().frame(frampath);
driver.findElement(By.name("firstname")).sendKeys("fdf");
// more operations
//statement1 (operation you want to perform)
//statement2(operation you want to perform)
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Switch to main page
driver.switchTo().defaultContent();

NoSuchWindowException - Selenium 3.4.0 - IE 11

I have launched IE 11 browser,
I have navigated to a initial URL --> done mouse over and clicked a link --> it redirects to another page.
In that page, I have to click a button, but it is throwing an exception
org.openqa.selenium.NoSuchWindowException: Unable to find element on closed window
but the windows still available on screen.
This is my code
WebElement e = driver.findElement(By.xpath("html/body/div[2]/div/div/header/nav/ul/li[2]/a"));
Actions a = new Actions(driver);
a.moveToElement(e).build().perform();
driver.findElement(By.xpath("//*[#id='menu-item-35']/a")).click();
TimeUnit.SECONDS.sleep(5);
// Exception is occurs after this, but when I delete the below code, the test case passes
driver.findElement(By.xpath("//*[#id='default_products_page_container']/div[3]/div[2]/form/div[2]/div[1]/span/input")).click();
This is the URL of the page: http://store.demoqa.com/
It looks to me like this is a race-condition error. I had those cases myself where I could actually see a window, but Selenium still said it wasn't there.
Have you tried setting the sleep time to a higher value?
You could also try to put a expect-wait-condition before clicking your element.
WebDriverWait wait = new WebDriverWait(DRIVER, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='default_products_page_container']/div[3]/div[2]/form/div[2]/div[1]/span/input"))
Here is the Solution to your Question:
A few words about the solution:
The xpath html/body/div[2]/div/div/header/nav/ul/li[2]/a looks petty vulnerable to me use linkText locator.
Once you use Action Class to build().perform(), induce a bit of wait before locating another element.
Instead of xpath locator of //*[#id='menu-item-35']/a element, use linkText locator.
Again the xpath //*[#id='default_products_page_container']/div[3]/div[2]/form/div[2]/div[1]/span/input looks petty vulnerable to me use a logical uniquexpath.
Here is your own working code block with some simple tweaks in it:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement e = driver.findElement(By.linkText("Product Category"));
Actions a = new Actions(driver);
a.moveToElement(e).build().perform();
driver.findElement(By.linkText("iMacs")).click();
driver.findElement(By.xpath("//input[#name='Buy']")).click();
Let me know if this Answers your Question.

How to ignore `other element will receive the click` and still click on the element

I am using Java and Selenium to write a test. I am going to click on a web element that is covered by another one so I receive the error other element will receive the click. I do NOT want to use Select so how can I click on the web element that is covered?
These are things that I have tried:
action.click(dropdown).build().perform();
action.moveToElement(dropdown).sendKeys(Keys.ENTER).build().perform();
dropdown.click();
Also there is no need to use scroll as the element is on the page but just covered by something else.
You can use JavascriptExecutor
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
I would not do this as it could miss potential defects. Not sure why you want to perform a click on hidden element.

DOM element is clicking in debuggin mode, but not in executing in Selenium WebDrvier

I have code to click on Radio button like this,
clickElement(By.xpath(properties.getProperty("radio.security.team.exists.outside.yes")), "RadioExistOutside");
clickElement is the generic method to click on element by xpath
I have taken this xpath and put it in FirePath, it found that element, inspected that element in command line and $p.click(); did this.. it is able to click on it... I think there is no problem with xpath...
I went to debugging mode and put debugging points over these lines, when I step over it, it is click on the element, but when I run the script, it is not able to click.
I thought that thread acceleration might be causing this and put a wait before it, but it didn't worked? please help me in this...
For xpath you need to share your HTML code so we can verify the xpath
While you can try with JavascriptExecutor to click your radio button
WebElement element= driver.findElement(By.xpath("YOUR Locator"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Note:- feel free to change the locator in above code according to your convenience.
Hope it will help you :)