i am using selenium page object model below is the problem scenario
i am clicking on an element using below command
driver.findElement(By.xpath("xxxxxxxx")).click();
when code execute the above line , click has been performed in the browser and a new page starts loading but code stuck at the above line untill the whole page loads.
i am not able to perform any operation on any webelemnet untill the page loads completely.
below is the code:
WebElement element = driver.findElement(By.xpath(".//*[#id='lll-menu-1']/div[1]/ul/li[4]/a"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);after this line code stuck here until the page loads completely
WebElement elem = driver.findElement(By.xpath(".//img[#alt='Cool Racerback - regular']"));
elem.click();
You can try with:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("YOUR ELEMENT XPATH")));
I don't think it is possible, you have to just wait for the page to load as this is built into webdriver.
Related
I have a standard form with 2 text boxes to fill and a submit button. The Submit button suppose to work after filling in the first mandatory text box.
It works manually, but when running on the automation infrastructure, the element doesn't get clicked.
The odd thing is that when debugging, the submit button is not clickable too, although it's not greyed out.
I tried the 3 classic methods:
Javascript:
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Actions:
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();
click:
element.click()
**Not working manually while in automation **
only when closing the form and creating a new one it works.
To click() on any clickable element ideally you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following solutions:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//elementXpath"))).click();
As an alternative, using Actions class:
new Actions(driver).moveToElement(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//elementXpath")))).click().build().perform();
Another alternative, using JavascriptExecutor:
((JavascriptExecutor)driver).executeScript("arguments[0].click();", new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//elementXpath"))));
According to your steps an replays , may be there is a hidden popup or something block your elements ,so use WebDriverWait not only clickable but try to elementToBeSelected() , invisibilityOfTheElementLocated() or
presenceOfAllElementsLocatedBy()
increase the waiting time to be able to detect the issues
I am new to the automation and I would like to know how to scroll to a web element on the current page using selenium and java.
I have tries many methods which is described in stackoverflow. But couldn't able to solve my problem.
Solutions I tried:
WebElement element = driver.findElement(By.id("id_of_element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(500);
You can use Actions class provided by Selenium.
public void scrollToElement(WebElement element){
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(element));
}
Here I have added an explicit wait and it will wait until the web element is visible. Maximum waiting time will be 60 seconds. If the web element is not visible within 60 seconds, this will throw an exception. You can increase the waiting time by changing this line.
WebDriverWait wait = new WebDriverWait(driver, 60);
Hope this helps.
To scroll a WebElement on the current page within the Viewport using Selenium and Java you need to induce WebDriverWait for the visibility of element located and you can use the following solution:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.id("id_of_element")));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element)
You're passing that element from javascript to java and back to javascript.
That's a bad idea, by the time it makes that round trip it might not be there anymore.
((JavascriptExecutor) driver).executeScript("document.querySelector('#id_of_element').scrollIntoView(true);");
Also, a lot of things that you needed to scroll into view for (with older seleniums), you no longer do.
I am not able to click on an element in selenium web driver getting error:
Cannot click on element (WARNING: The server did not provide any stack trace information)
This problem is only on IE and everything works fine on Firefox.
I used isDisplayed() function but it's not showing element. Maybe opacity of the element is zero?
If the element is not visible you cannot click on it through normal ways. You could execute a javascript script to click on it though.
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
It may be related to fact that object was not loaded yet, you can wait until button is displayed.
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("id"))).click();
I am using following xpath to click on element using JSExecutor in Selenium webdriver. This works fine in Firefox and chrome but does not work in IE.
Any idea to make this work? After lot of trial and error I have made this work in FF and chrome and have come up with the following XPath.
//*[contains(#class,'ui-select-choices-row') or contains(#id,'ui-select-choices-row')]//*[text()='TextofMyElementToBeclicked'
Additional info: This is a Jquery drop down on an angularJS application. When the user clicks on the drop down //ul is loaded and i am using the above xpath (which is part of //ul) to select the element based on text (using Javascript executor click). I used JS executor because, click() function in selenium simply could not click on the drop down element.
I am clicking element using below.
WebElement element = driver.findElement(By.xpath("YourNumbersXpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
enter code here
I successfully tested your XPath with IE11, so it's not an issue related to IE. It's most likely a timing issue. First click on the drop button, then wait for the targeted element to appear and finally click on it:
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.get("...");
// move the cursor to the menu Product
WebElement element = driver.findElement(By.xpath("drop down button")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("drop down item"))).click();
IE11 seems to struggle with contains(#class and possibly also the contains(#id. Try using alternative solutions like starts-with.
I am new in Selenium. I am using Selenium WebDriver with Java. I'm using eclipse as IDE. I have written some code for Login page and it is run successfully. Now I want to go to desired page after successful login, but I want to wait for few time before transiting another page. How can I wait a page before loading another page?
As far as I know, there are 3 ways:
Implicit wait: (It's applicable for all elements on the page)
driver.manage().timeouts().implicitlyWait(A_GIVEN_NUMBER, TimeUnit.SECONDS);
Explicit wait: (Applicable for a particular element)
WebDriverWait.until(CONDITION_THAT_FINDS_AN_ELEMENT);
More specific code is as below:
WebDriverWait wait = new WebDriverWait(driver, 40);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
Using Thread:
Thread.sleep(NUMBER_OF_MILLIS);
I really would advise against using Thread.sleep(NUMBER_OF_MILLS). It will not be stable and you will hit occasions when the sleep is not long enough.
If you are simply waiting for the DOM to load, then a WebDriver event which triggers page load will always wait for the DOM to load before returning control.
However, if AJAX is used to change the HTML after DOM, then I would advise you to use WebDriverWait, and wait until a known event happens (e.g. Object appears in html, text changes, etc.)
If you take one thing away from this post, then please stop using Sleep!
Use class WebDriverWait
Selenium explicit / implicit wait
You can wait until the element you are expecting on the next page comes up. :
WebDriver _driver = new WebDriver();
WebDriverWait _wait = new WebDriverWait(_driver, TimeSpan(0, 1, 0));
_wait.Until(d => d.FindElement(By.Id("Id_Your_UIElement"));
Try by using implicitlyWait for 60 sec. as follows in Selenium WebDriver:
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);//wait for 60 sec.
If you need to wait more make it 80, 90 and so on.
For Selenium RC you can use the code as below:
selenium.waitForPageToLoad("60000");//wait for 60 sec.
It can be done by using Thread as below:
Thread.sleep(NUMBER_OF_MILLIS);
For explicit wait in WebDriver, identify an element in loading page and write the code as below:
WebDriverWait wait = new WebDriverWait(driver, 40);//wait for 40 sec.
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
I have used this approach. Try to figure out which element on the page is the last to load. By taking the locator of that element and checking it's existance using isDisplayed, you will be able to see when the entire page loads
Implicit Wait:
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
Explicit Wait:
WebDriverWait wait = new WebDriverWait(driver, 40);
This code is deprecated in Selenium 4.
Instead, use this,
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(60));
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(40));