Selenium Webdriver -- No such Element - selenium

I am trying to login into Sears.com using Selenium webdriver.clicked on Sign in link-->login form opens.
But unable to locate the text box element inside the login form. The login form is inside an iframe (frame Name =easyXDM_default5914_provider).This iframe is inside div (id=modaliframe)
driver.switchTo().frame(driver.findElement(By.xpath(".//iframe[#id='easyXDM_default5914_provider']")));
driver.switchTo().activeElement();
driver.findElement(By.id("email")).sendKeys("xxx#gmail.com");
Getting below exception in my console:
org.openqa.selenium.NoSuchElementException: Unable to locate element:
{"method":"id","selector":"email"}

Try with the below code.
driver.get("http://www.sears.com/");
driver.findElement(By.xpath("//*[#id='header-shop-your-way-partner']")).click();
driver.findElement(By.xpath("//*[#id='open-sign-in-form']/span[2]")).click();
driver.switchTo().frame("registration-form-iframe");
driver.findElement(By.xpath("//*[#id='email']")).sendKeys("abcd#gmail.com");
Thread.sleep(3000);

Related

Unable to locate element | xPath | Selenium Webdriver

I am trying to reference and then click an element on a web page.
This is the web page: https://www.facebook.com/settings
This is the element:
<div class="linkWrap noCount">Notifications <span class="count _5wk0 hidden_elem uiSideNavCountText">(<span class="countValue fsm">0</span><span class="maxCountIndicator"></span>)</span></div>
It's a DIV which contains the word "Notifications"
We should be able to reference with a simple xPath. Such as //div[contains(text(), 'Notifications')]
However, it doesn't seem to work.
A chrome extension I use says the element doesn't exist.
My code can't find the element.
Here is my Java code using Selenium web driver.
// Navigate to page
obj.driver.get("https://www.facebook.com/settings");
// Write to console where we are
System.out.println(obj.driver.getCurrentUrl());
// Wait | Plenty of time for the page to load
Thread.sleep(5000);
obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]")).click();
Here is the error:
It is so very strange! Any ideas on why I can't reference the element, or why the xPath doesn't exsist.
Your element is within an iframe.
If you scroll up from where you are you'll see this:
For selenium you need to switch frames in order to access the elements within them
I've not tried it for your site yet - but you switch frames with:
driver.switchTo().frame(1); // by index
//<or>
driver.switchTo().frame("id of the element"); //by id
//<or>
driver.switchTo().frame(element); // be element
Then, when ready switch back to the main page/frame with:
driver.switchTo().defaultContent();
Have a look at the frames section of the selenium docs here

Getting an error when clicking a link element on a web page by a Selenium script

When my Selenium script clicks a link element presented on a web page by click() method, I am getting the below error:
org.openqa.selenium.WebDriverException: Element is not clickable at point (36, 72).
This is my HTML code
<div id="targettab">
Book
</div>
This is my Selenium code:
driver.findElement(By.id("highlight-book")).click();
What am I doing wrong here? Could you please advise me about possible solutions? Thanks.
If element is present in lot of nested divs, rarely selenium driver will fails to click element. You can try clicking using Enter button.
driver.findElement(By.id("highlight-book")).sendKeys(Keys.Return);

Click method is not working when using JavascriptExecutor in Selenium

I am clicking on the text box displayed on first page
WebElement txtBox = driver.findElement(By.xpath("---xpath---"));
txtBox.click();
Then after some block of execution I am getting the same textbox in new page on same window.
Here also I want to click on the text box.
I used JavascriptExecutor to do this scripting.
((JavascriptExecutor)driver).executeScript("arguments[0].click();", txtBox );
But while running the script I am getting an error message saying:
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
You need to execute driver.findElement() every time you reload the page.
This method returns WebElement which is integral part of current page. If you refresh browser or navigate to some other URL or even if element is deleted and attached again by some javascript on same page, previously found element cannot be used anymore.
Here you have official explanation of this exception: http://www.seleniumhq.org/exceptions/stale_element_reference.jsp
Stale Element exception comes after you want to interact with an element loaded previously. If you get webelement and then reload the page it gives this exception because it is not in a newly created page. It is better to click the web element without assigning it to a variable like:
driver.findElement(By.xpath("---xpath---")).click();

Using Selenium Chrome Driver to search elemets by xpath is not working

I am running my Cucumber Test using Selenium and using Chrome driver. I am trying to get a div element with xpath but it is not working.
It is the html element:
<div class="row" id="headerspacing"></div>
And it is my test code:
WebElement div = agent.findElement(By.xpath(".//*[#id='headerspacing']"));
Assert.assertNotNull(div);
But I am getting an error message with the element:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":".//*[#id='headerspacing']"}
Update: new tab window
In the previous steps declared in my Cucumber tests Selenium hit a link (tag element a) and it step is opening a new tab windows. I thought Selenium was searching into that page and it is the problem: It was looking into the current page instead of new tab.
So maybe my question:
How do I search or move my driver in order to do a search into the new open tab windows?
If the div is in a new window (tab), you must switch to that window before you can locate any elements on it. So, before trying to find the element, you want to do:
switchTo().window("windowName");

Trouble locating a checkbox in an iframe with Selenium Web Driver

I'm trying to locate a checkbox in an iframe in a Javascript based website using Web Driver and python. I've tried locating by ID and XPATH and neither seem to work since I must be looking in the wrong frame. The checkbox is visible on the page to the user and selenium IDE also seems to come up with the same answer as me, but it still results in 'NoSuchElementException: Message: u'The element could not be found'
html for checkbox: input type="checkbox" onclick="CE.CESECUR.onClickFullSecurity()" id="cefullsecure"
selenium code:
_settings_ssl_locator = (By.XPATH, ".//*[#id='cefullsecure' and onclick='CE.CESECUR.onClickFullSecurity()']")
def click_settings_enable_ssl(self):
self.selenium.find_element(*self._settings_ssl_locator).select()
I think you you would need to switch to the frame first and then locate the element, do the task/action on the elements present on the frame and switch back to the previous frame.
WebElement checkBoxframe = driver.findElement(By.tagName("enter frame name here"));
driver.switchTo().frame(checkBoxframe);
//write your selenium code here for checkbox
driver.switchTo().defaultContent();