Getting Stale Element Reference Exception while trying to menu links in a web page - selenium

I am trying to click the main menu items in seleniumhq.org but after clicking on the first link i am getting a StaleElementReferenceException:Element not found in the cache=perhaps the page was changed since it was looked up
Please provide solution to solve the above problem
Below was my code
WebDriver d=new FirefoxDriver();
d.get("http://docs.seleniumhq.org/");
d.manage().timeouts().implicitlyWait(100,TimeUnit.SECONDS);
List<WebElement> l=d.findElements(By.cssSelector("ul>li"));
for(WebElement e:l) {
e.click();
}
Thanks in advance

If you click on a link and you are taken to the different page or even if you stay in same page the DOM is refreshed. Those elements are not attached to DOM anymore. You need to write some code to come back to previous page if your click takes you to a different page or even if you stays in same page you have find the link on the fly to click instead of "e.click()"

Related

Page elements seen in the browser dev tools are not retrieved by ChromeHeadless

In this page :
https://www.bedbathandbeyond.com/store/product/o-o-by-olivia-oliver-turkish-modal-bath-towel-collection/5469128?categoryId=13434
I can see a button with "Add to Cart" text , I can also see it in dev tools.
But when the same page source is retrieved by ChromeHeadless using selenium, and my script searches for it, this text is not present.
I tried with selecting show page source in the browser, the source too did not have the "Add To Cart text"
Further I used a curl to GET page, "Add To Cart" wasn't in the returned page source either.
What am I doing wrong?
is the page hiding the button?
How can I check for its presence, for product availability check?
The elements you are looking for are inside the shadow DOM. You need to access the shadow root first. Hard to see exactly what is going on in the DOM without some trial and error, but something like this:
WebElement shadowHost = driver.findElement(By.cssSelector("#wmHostPdp"));
SearchContext shadowRoot = shadowHost.getShadowRoot();
WebElement addToCart = shadowRoot.findElement(By.cssSelector(".shipItBtnCont button"));
More info on Shadow DOM & Selenium — https://titusfortner.com/2021/11/22/shadow-dom-selenium.html

How to select an element of a pop up prompt using Selenium

I'm writing a code using Selenium and Chromedriver to remotely control my instagram.
I've managed to login to the platform however as soon as I do a pop up asking about activating notifications appears and my codeflow breaks because it can no longer click the elements of the instagram page.
this is the code I tried using (which works during login to click the "send" button
notifBtn= self.browser.find_element_by_css_selector('button')
notifBtn.click()
(since it's the first instance of button on the web code it should automatically select that one)
however this time the browser is stuck and doesn't do anything.
during the login I had a problem where it wouldn't click either and found that it was because there was a lag between the request to go to the instagram login page and loading the page so it could find the CSS elements. I fixed it by adding
time.sleep(2)
before inputting the data and it fixed. I thought something similar would work here as it doesn't load instantly but it made no difference.
Is this an issue with the selector, as in could I use xpath to get around it? anyways sorry if it's a bit vague and feel free to ask me about my code or whatever.
notifBtn= self.browser.find_element_by_css_selector('button')
notifBtn.click()
I expect Chromedriver to click don't activate in the pop up so the code can continue instead I get stuck on that screen
Your find_element_by_css_selector call is probably locating multiple elements, and doesn't know which one to click. I would use an XPath here, to be more explicit about which button you are trying to click:
from selenium.webdriver.support import expected_conditions as EC
# Wait for button to exist
not_now_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[text()='Not Now']")))
# Click the button
not_now_button.click()

How to sendKeys in textBox? for automation

I'm doing a test on c# with selenium for this site http://onliner.by.
At first I have to be authorized on this site.
I found(by xpath) button with name"Вход" in the right upper corner and click on it.
Then there's refreshed and page changed, but the link remained the same (http://onliner.by).
And I need to enter login and password on this page and sumbit it. But I cannot do it.
I founded Xpath paths of this elements and I used this code:
//this doesn't work
driver.FindElement(By.XPath("//*[#id='auth-container__forms']/div/div[2]/form/div[1]/div[1]/input")).SendKeys("user");
driver.FindElement(By.XPath("//*[#id='auth-container__forms']/div/div[2]/form/div[1]/div[2]/input")).SendKeys("password");
driver.FindElement(By.XPath("//*[#id='auth-container__forms']/div/div[2]/form/div[3]/div/button")).Click();
How can I do it? I tried to use SwitchTo().Frame but it didn't help too.
I will be very grateful for help.
You have to wait element availablity like this
new WebDriverWait(driver, TimeSpan.FromSeconds(timeOut)).Until(ExpectedConditions.ElementExists((Locator Value)));

How to handle Selenium Element not found in the cache Exception

nextbutton.Click(); //Click action on current page
System.out.println("entered next page"); // validating code on above click action. reading this line ensures above line got executed successfully.
driver.findElement(By.id("CardName")).clear();//Element in next page.
getting exception as Element not found in the cache - perhaps the page has changed since it was looked up.
Unable to identify root cause, i am able to see my web page is taken to next page(page flow visible) on nextbutton.Click() action, but in console my check out.s.o.p"System.out.println("entered next page");" is not printed.
Could some one please help me on this.
I tried, wait, refresh, sleep, for loop for identifying element.. issue i identified is script is being terminated at click action but where as on web page i am able to see next page getting loaded.
Element not found in the cache - perhaps the page has changed since it
was looked up.
this exception happends when you initialize some collection with webelements, the page is changed and the collection not initialize again.
look where you have something that was initialize before the click and you want to use it after, for proper use you need to initialize it again.

How to handle popup window using selenium webdriver with Java

Please help, I'm new in Selenium. I try to automate eCommerce website and I have problem to handle popup window. Here is the scenario:
Go to http://www.lampsplus.com
Click on Sale link in the header section.
Click on the 1st item/product displayed on the page. (This will show the product page).
On the product page, click on the red Add To Cart button. (This will add a product to cart and display a popup).
On the popup, click the dark-grey Continue Shopping button. (This will close the popup.)
I stuck on step 5 (Error message: Unable to locate element "Continue shopping button")
Here is my code before step 5:
WebDriver d1 = new FirefoxDriver();
d1.manage().window().maximize();
d1.get("http://www.lampsplus.com");
d1.findElement(By.name("hdr_sale")).click();
d1.findElement(By.xpath(".//*[#id='sortResultContainer60238']/a[2]/span")).click();
d1.findElement(By.id("pdAddToCart")).click(); //This is step 4
//Here is suppose to be some code which handles the popup - my problem
d1.findElement(By.id("aContinueShopping")).click(); //This is step 5
I'm aware about .getWindowHandle(); method. I tried several variations of it and none of them worked.
Can anyone give me an idea how to handle it. Many thanks! I use Java.
Note: I don't work for LampsPlus and not try to promote their products, this website was chosen for training purposes only.
The element aContinueShopping is contained within an iframe.
You'll have to switch to the iframe using:
WebElement frameID = d1.findElement(By.Css("#add-to-cart>iframe"));
d1.SwitchTo().Frame(frameID);
d1.findElement(By.id("aContinueShopping")).click();
There's no 'name' or 'id' on the iframe, so you'll have to use a WebElement or a numeric to find it.
Once you're done with that iframe, you'll switch back to 'top' by using:
d1.SwitchTo().DefaultContent();