Selenium Webdriver: Element is not currently visible on running through Jenkins - java

Basically I want to click the on a element that became available after clicking on another element:
WebElement element;
element = driver.findElement(By.xpath("//span[#class='btn-expand btn-exp-right hide-text']"));
element.click();
// Below is the explict wait that will wait max 45 seconds for below element. And this element will be only be available after clicking above one.
action.explictWait(45, By.xpath("//span[#class='btn-plus']"));
element = driver.findElement(By.xpath("//span[#class='btn-plus']]"));
// Error this encounter on clicking.
element.click();
While running through Jenkins I am unable to click "//span[#class='btn-plus']]". However this is working fine while running locally.
Please reply soon.

Go to the jenkin server and launch the page and see if the button is visible.

Related

Can not click on a button; - "element is not interactable"

I have a script where I'm trying to push a button on an ecommerce site but when I run a script I get "Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable " .
When I put xpath into the chropath, element is located so I guess path driver.findElement(By.xpath("//form/fieldset[2]/button")).click(); is right. Please see attached screenshots and be gentle I'm new to programming and this site :/
I see, there's an Accept cookies button, if you want to click on that, you can use the below code :-
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#onetrust-accept-btn-handler"))).click();
and then click on Sleeve length like this :
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//form/fieldset[2]/button"))).click();
Note that you should open browser in full screen before interacting any web element.
driver.manage().window().maximize();
driver.get("Your URL");
You could use a JavascriptExecutor to trigger the click:
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", yourWebElement);
But I would recommand also to use explicit/implicit waits instread of Thread.sleep (I've linked an article, you can find more information on google).

Page is unclickable after closing a window

My flow is as follows in selenium:
Access a webpage
Click a tab
Click on add button in a tab where a window would open
Close that window
Click on that same tab again
I'm able to go through steps 1 to 4 without issues however at step 5 i'm not able to click the tab element knowing that i've clicked that same tab at step2, I did check from console and the same xpath I used in step2 did not return any element however when i clicked on that element to inspect it in console it started returning some values in console but still didn't work from selenium when i continued the run (in debug)
My page is in an Iframe which i was successfully able to access and print page title after step 4 but my issue remains, why aren't i able to click that tab from selenium after i close the window and the screen refreshes, why are the elements unresponsive anymore?
After doing some research i also tried the below script:
WebElement element=driver.findElement(By.xpath(the_path));
JavascriptExecutor ex=(JavascriptExecutor)driver;
ex.executeScript("arguments[0].click()", element);
but it would fail at the first line since the element is not fount, tried to initialize it earlier in the code but then it would fail at line 3.
How do i fix this issue?
Try to click on the element using Selenium code, not by injecting JavaScript to the pages. This should solve your problem.
WebElement element = driver.findElement(By.xpath(the_path));
element.click();
The issue was due to having multiple Iframes in the main page, so when closing switching windows I was not redirected to the proper Iframe in which i was in step 3 when clicking the tab.

Can`t mark the elements in Div pop up in chrome while using Selenium automation

While automating in automationpractice.com,
Steps:
1.open "automationpractice.com/index.php?id_product=5&controller=product#/size-s/color-orange"
2. click "Add to Cart" button
3. Below add to Cart successfully pop-up displayed(Refer to below screenshot)
Now I cannot mark any elements in this pop up. I tried XPath which is inside the parent window with Div tag, but The error is ElementNotVisible. I tried it with driver.getWindowHandles(), but it shows that only 1 window exists(Parent window). Also tried with alert, but it says no alert exists.
This issue only occurs in the Chrome browser, for firefox , it works fine using xpath inside parent window with Div tag.
Below is the script I tried, can anyone kindly help on this? Thank you in advance!
The xpath for this pop up is "//*[#id="layer_cart"]/div[2]"
Set<String> windows = wd.getWindowHandles();
System.out.println("windows.size():"+ windows.size()); // the result is 1
There is no new window or alert on this page, only problem is your XPATH cotains " instead of ', alo need to wait a few seconds for that element to be visible:
WebDriver driver = new ChromeDriver();
driver.get("http://automationpractice.com/index.php?id_product=2&controller=product");
driver.findElement(By.xpath("//form[#id='buy_block']//p[#id='add_to_cart']")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='blockcart_caroucel']/li[1]")));
driver.findElement(By.xpath("//*[#id='blockcart_caroucel']/li[1]")).click();

Java Selenium: Get get multiple WebElements of the same class

This is the Button I want to press:
<button class="ProfileTweet-actionButton js-actionButton js-actionRetweet" data-modal="ProfileTweet-retweet" type="button">
The same kind of Button exists multiple time on the site.
I tried pressing it by doing this:
By byaXpath = By.xpath("//div[contains(#class,'ProfileTweet-actionButton js-actionButton js-actionRetweet')]");
WebElement Element = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(byaXpath));
Element.click();
But after 10 secons I just get this error:
org.openqa.selenium.TimeoutException: Timed out after 10 seconds waiting for presence of element
Im kinda lost now. I tried diffrent solution to similar cases but nothing seems to work.
You have to put button instead of div at the start of your xpath.
"//button[contains(#class,'ProfileTweet-actionButton js-actionButton js-actionRetweet')]"

Selenium -- How to wait until page is completely loaded [duplicate]

This question already has answers here:
Wait for page load in Selenium
(48 answers)
Closed 6 years ago.
I am trying to automate some test cases using Java and Selenium WebDriver. I have the following scenario:
There is a page named 'Products'. When I click on 'View Details' link
in the 'Product' page, a popup (modal-dialog) containing the details of the item appears.
When I click on the 'Close' button in the popup the popup closes and
the page automatically refreshes (the page is just reloading, the contents remain unchanged).
After closing the popup I need to click on 'Add Item' button in the
same page. But when WebDriver trying to find the 'Add Item' button,
if the internet speed is too fast, WebDriver can find and click the
element.
But if the internet is slow, WebDriver finds the button before the
page refresh, but as soon as the WebDriver click on the button, the page refreshes and StaleElementReferenceException occurs.
Even if different waits are used, all the wait conditions become true
(since the contents in the page are same before and after reload)
even before the page is reloaded and StaleElementReferenceException
occurs.
The test case works fine if Thread.sleep(3000); is used before clicking on the 'Add Item' button. Is there any other workaround for this problem?
3 answers, which you can combine:
Set implicit wait immediately after creating the web driver instance:
_ = driver.Manage().Timeouts().ImplicitWait;
This will try to wait until the page is fully loaded on every page navigation or page reload.
After page navigation, call JavaScript return document.readyState until "complete" is returned. The web driver instance can serve as JavaScript executor. Sample code:
C#
new WebDriverWait(driver, MyDefaultTimeout).Until(
d => ((IJavaScriptExecutor) d).ExecuteScript("return document.readyState").Equals("complete"));
Java
new WebDriverWait(firefoxDriver, pageLoadTimeout).until(
webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
Check if the URL matches the pattern you expect.
It seems that you need to wait for the page to be reloaded before clicking on the "Add" button.
In this case you could wait for the "Add Item" element to become stale before clicking on the reloaded element:
WebDriverWait wait = new WebDriverWait(driver, 20);
By addItem = By.xpath("//input[.='Add Item']");
// get the "Add Item" element
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(addItem));
//trigger the reaload of the page
driver.findElement(By.id("...")).click();
// wait the element "Add Item" to become stale
wait.until(ExpectedConditions.stalenessOf(element));
// click on "Add Item" once the page is reloaded
wait.until(ExpectedConditions.presenceOfElementLocated(addItem)).click();
You can do this in many ways before clicking on add items:
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.elementToBeClickable(By.id("urelementid"))); // instead of id you can use cssSelector or xpath of your element.
or:
wait.until(ExpectedConditions.visibilityOfElementLocated("urelement"));
You can also wait like this. If you want to wait until invisible of previous page element:
wait.until(ExpectedConditions.invisibilityOfElementLocated("urelement"));
Here is the link where you can find all the Selenium WebDriver APIs that can be used for wait and its documentation.
yes stale element error is thrown when (taking your scenario) you have defined locator strategy to click on 'Add Item' first and then when you close the pop up the page gets refreshed hence the reference defined for 'Add Item' is lost in the memory so to overcome this you have to redefine the locator strategy for 'Add Item' again
understand it with a dummy code
// clicking on view details
driver.findElement(By.id("")).click();
// closing the pop up
driver.findElement(By.id("")).click();
// and when you try to click on Add Item
driver.findElement(By.id("")).click();
// you get stale element exception as reference to add item is lost
// so to overcome this you have to re identify the locator strategy for add item
// Please note : this is one of the way to overcome stale element exception
// Step 1 please add a universal wait in your script like below
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // just after you have initiated browser
There are two different ways to use delay in selenium one which is most commonly in use. Please try this:
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
second one which you can use that is simply try catch method by using that method
you can get your desire result.if you want example code feel free to contact me defiantly I will provide related code

Categories