Submit button is not getting clicked after executing the selenium script - selenium

The Submit button is not getting clicked after executing the selenium script.I have attached the screenshot of the code.

You can introduce explicit wait. Something like this should do the Job.
new WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(By.name("websubmit"))).click();
I'm using name attribute over xpath because of stability and precedence given.
Hope this will help.

To improve overall script, you can add implicit wait for your entire webdriver instance. Currently I can see no wait is there for any element so in future your script may fail if any element get stuck/delayed in loading.
Try using -
fd.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); Add this wait after your webdriver instance creation.

Related

WebElement click() not working in Selenium

I was learning automated selenium testing in https://www.yatra.com/etw-desktop/. While trying to click an image button named 'Asia'(image is attached) ,I am getting a Time out exception. Please help me in figuring out what's going wrong .
driver.manage().window().maximize();
driver.get("https://www.yatra.com/etw-desktop/");
driver.manage().timeouts().implicitlyWait(4000, TimeUnit.MILLISECONDS);
Thread.sleep(5000);
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable
(By.xpath("//*[#id=\"scrollable1\"]/div[1]/div/a[2]/div[4]"))).click();
Thread.sleep(4000);
Assert.assertEquals("https://www.yatra.com/etw-desktop/city-list",driver.getCurrentUrl());
image showing the web element to be clicked
Try this:
How to Find Web Elements in Shadow DOMs using Selenium
It is most probably because of using Thread.sleep(), which is the worst case of explicit wait. Instead use the wait method provided by selenium itself.

Browser runs faster than webdriver selenium command

I am performing an operation using selenium webdriver to wait for an element until an element is visible. After a few milliseconds, it gets disappeared(Expected).Generally we use explicit wait to synchronize with browser because browser is slower. But in this case, browser is faster and before command waits for the visibility , the element disappears hence failing the operation.
It would be great if anyone can help regarding the issue.
PS I am using jmeter webdriver plugin.
Thanks.
You could handle exception which breaks your validation (ignore NoSuchelementException but fail validation on TimeoutException) or create waiting method which waits for element to appear and after that wait to disappear.

Selenium webdriver Not able to click on log out

driver.findElement(By.linkText("Log Out")).click();
I used above line of code to logout.Until yesterday it was working but today it is not.
I have used below xpath:-
driver.findElement(By.xpath("/html/body/div/div[3]/div[1]/div/ul[2]/li/div/ul/li[3]/a")).click();
It is still not working,
Later I found that I focus on logout option than it does the operation of logout.
Why is it so?
and why it was working yesterday but not today?
One possible issue may be that your browser is loading cached page that has broken logout button . You need to give more strict Xpath and wait to make sure the whole page load
"/html/body/div/div[3]/div[1]/div/ul[2]/li/div/ul/li[3]/a"
Make it strict something like -
driver.findElement(By.xpath("//Button[text()='logout']")).click();
Try to use
driver.findElement(By.partialLinkText("Log Out")).click();
Or
driver.findElement(By.xpath("//a[contains(text(),'Log Out')]")).click();
A stack trace might be very helpful to find a solution for you.
It could be, that
the dropdown menu entry for log out needs short time to become visible?
Selenium acts like a user and can interacts with visible elements.
You could click on the dropdown use WebDriverWait.Until(..) to wait until it appears and use it
or
Another way might be navigating directly to log out url.
the link text has changed (blanks, capital letters ..)
Try doing this -
WebElement element = driver.findElement(By.linkText("Log Out"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

Selenium, what is the best practice to deal with element is not clickable

I am using selenium 2.46 (firefox driver) to develop an application. There are a lot of element.click() in my code. Sometimes that elements are not visible or not clickable make the application throws selenium exception.
To resolve that issue, i use WebdriverWait(driver, 10).until(...) for each single element which needs to be clicked.
My question is there is any other better way Or design pattern that can help me to solve the problem best.
Or at least i dont have to use WebdriverWait for each single element needs to be click().
You cannot avoid WebDriverWait. If you send a webdriver click command, webdriver will blindly assume that "element is clickable". You need to instruct webdriver to wait because your element is special and needs some synchronization before it can click on it. I don't think you need to do this for every other element. You can incorporate ExpectedConditions so that you can keep your code snippets manageable and small. So something like,
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("foo"))).click();
The other option you can try other than clicking is hit enter on respective element, for that you can refer ID of that element.
driver.findElement(By.id("elementid")).sendKeys(Keys.ENTER);
use implicit wait instead of explicit wait and give the expected condition till the element doesn't visible on screen.
for more info you can check
http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#invisibilityOfElementLocated-org.openqa.selenium.By-
Hope this will help you

Selenium WebDriver. After waiting for the element click() freezes test

I need to click on the dynamically generated element using Selenium WebDriver (Java API).
I'm waiting for this element to appear with WebDriverWait and then clicking on it. This click succeeds but the following click on the different static element freezes the whole test. Here is the code:
webDriver.get(alfrescoURL + "/share/page/create-document");
WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement documentTypeList = webDriver.findElement(By.id("template_x002e_create-document_x002e_create-document_x0023_default_documenttype-selected-form-button-button"));
documentTypeList.click();
WebElement listItem = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("yui-gen100")));
listItem.click();
// Choosing to create in new project
WebElement projectLink = webDriver.findElement(By.id("template_x002e_create-document_x002e_create-document_x0023_default_projecttype-entry1"));
projectLink.click();
documentTypeList.click() opens a drop-down list, listItem.click() chooses an item, projectLink.click() makes a choice in the group of radiobuttons. Test silently freezes on projectLink.click(). It looks like this click() infinitly waits for page reloading that happens by some reason while it shouldn't. (Disappearing of the list after choosing an item is made by javascript that doesn't make any AJAX requests.)
I think there is something about click() blocking i don't understand. It says in it's javadoc that it attempts to block only if it causes a page to load. Nevertheless here i get a block for some reason.
If i insert a thread sleep before projectLink.click() then test works fine. It agrees with a hypothesis that i get a infinite block on click().
Thanks in advance.
I've run into this before where the test runs faster than the drop down can contract and can't click the following element. Instead of using arbitrary sleeps (although in rare cases they are necessary), can you put in a wait for a class change in the drop down?
For example, if I want to wait for the drop down to contract before moving on, I'll wait for the class of the select to change from "active" to "closed". This, of course, assumes your HTML has these dynamic classes in place.
Another possibility is to set an implicit wait, giving yourself enough padding for instances like these:
driver.manage().timeouts().implicitlyWait(1000, TimeUnit.MILLISECONDS);
I would suggest that you try other click options too:
a) Actions#click()
b) Javascript click()
If any of those click works then it means the issue is with the selenium's WebElement Click method which needs to be reported.