Unable to click submit button in selenium - selenium

I am working in a travel application. Once i filled the details like origin, destination i have to click the submit button. I tried with the xpath to click the submit button. But, i am unable to submit. Application is loading internally,once it clicks the xpath of submit. Please refer screen shot.
Selenium code: java driver.findElement(By.xpath(".//[#id='ctl00_cphMain_TravelRequest1_btnSubmit']")).click();
Please check image
Submitbutton- xpath code

can you please provide your html code for this issue,
there must be element present in that code,
you should have to use submit() instead of clicking on submit element.
actually click() works only on submit button of (you have to check this condition) and submit() works for all form elements and performs Enter key pressed like operations.
i hope this have to work for you,
driver.findElement(By.xpath(".//[#id='ctl00_cphMain_TravelRequest1_btnSubmit']")).submit();
or try using this,
driver.findElement(By.xpath(".//[#id='ctl00_cphMain_TravelRequest1_tpTravel_travel_gvTravel_ctl02_txtDuration']")).submit();
according to your html code we get element by id,
driver.findElement(By.Id("ctl00_cphMain_TravelRequest1_btnSubmit")).submit();

In your case, might be the submit button is visible but not clickable as you mentioned in the description. For that, you will have to wait until the element is not to be clickable using the ExplicitWait method. Perform click action after the Submit button gets clickable:
Hope below code will solve your problem.
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//[#id='ctl00_cphMain_TravelRequest1_btnSubmit']"))).click();

Sometimes the click() method does not work. Please try with below code once and let me know.
WebElement Login=driver.findElement(By.id("ctl00_cphMain_TravelRequest1_btnSubmit"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", Login);

Related

How to click on this button with Selenium but the id is not unique

<button value="1" class="_42ft _4jy0 layerConfirm uiOverlayButton _4jy3 _4jy1 selected _51sy" type="submit">Confirm</button>
The above is the html code for a button on Facebook page. I would like help in selenium web driver to click on this button but the id of the tag is changing on every refresh
Code on facebook website
Thank you all for your help but I have solved this one with help from you all people and trial and error.
Use ExplicitWait until visibility of your element and then perform click
WebDriverWait wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[#type='submit'][normalize-space()='Confirm']")));
driver.findElement(By.xpath("//button[#type='submit'][normalize-space()='Confirm']")).click();
Another solution can be if some javascript preventing you to click the button then use below JavascriptExecutor code and let me know if any issue
WebElement button = driver.findElement(By.xpath("//button[#type='submit'][normalize-space()='Confirm']"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", button);
You can use the fixed class names for this
driver.findElement(By.className("layerConfirm"));
Or combination
driver.findElement(By.cssSelector(".layerConfirm.uiOverlayButton"));
If the dynamic id has fixed part you can use partial id
// id=someid134313
driver.findElement(By.cssSelector("[id*='someid']"));
Or by the "Confirm" text
driver.findElement(By.xpath("//button[contains(., 'Confirm')]"));
As Facebook element's class and id are dynamic, you will have trouble automating further with Selenium.
In this case you can go with xpath
//button[#type='submit' and text()='Confirm']
or
//button[text()='Confirm']
You can use following xpath for your case:
driver.findElement(By.xpath("//*[contains(text(),'Confirm')]")).click();
Hope it will help you.

How to click on button when input type =Button in Selenium Web driver by using Java

How to click on button when input type is button , I am using below code, click on button is working but data is not saved.
driver.findElement(By.cssSelector("input[type='button'][value='Save']")).click();
driver.findElement(By.cssSelector("input[#type='button'][#value='Save']")).click();
driver.findElement(By.cssSelector("input[#type='button']")).click();
And below is the development code for your reference:
<input id="save_btn_expe" class="edit_forms_save_btn" type="button" value="Save">
WebElement setElement = driver.findElement(By.id("save_btn_expe"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click()", setElement);
OR
driver.findElement(By.id("save_btn_expe")).click();
OR
driver.findElement(By.xpath("//input[#id='save_btn_expe']")).click();
By considering your statement "click on button is working". You can use the submit method to post form data. You can use two other alternates for click. Following code is in c#:
driver.FindElement(By.Id("save_btn_expe")).submit();
// Send enter key on the element.
driver.FindElement(By.Id("save_btn_expe")).SendKeys(OpenQA.Selenium.Keys.Enter);
Before trying the above code, check manual save to make sure there is no bug on save.
Since it's an input tag element, though the type is button .click() or .submit() will not work. You have to provide the Enter key as an input. Use following code :
driver.findElement(By.xpath("//input[#id='save_btn_expe']")).sendKeys(org.openqa.selenium.Keys.ENTER);
I hope this will work for you, as it did for me.
if using for in DOM, the selector below work for me :
Selenide.$("label[for=\"save_btn_expe\"]").click();

DOM element is clicking in debuggin mode, but not in executing in Selenium WebDrvier

I have code to click on Radio button like this,
clickElement(By.xpath(properties.getProperty("radio.security.team.exists.outside.yes")), "RadioExistOutside");
clickElement is the generic method to click on element by xpath
I have taken this xpath and put it in FirePath, it found that element, inspected that element in command line and $p.click(); did this.. it is able to click on it... I think there is no problem with xpath...
I went to debugging mode and put debugging points over these lines, when I step over it, it is click on the element, but when I run the script, it is not able to click.
I thought that thread acceleration might be causing this and put a wait before it, but it didn't worked? please help me in this...
For xpath you need to share your HTML code so we can verify the xpath
While you can try with JavascriptExecutor to click your radio button
WebElement element= driver.findElement(By.xpath("YOUR Locator"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Note:- feel free to change the locator in above code according to your convenience.
Hope it will help you :)

Selenium webdriver script to enter an url to an element and press enter key

This element is present in a contact form page it is like an uploading url where we need to enter the url of the image and press enter
The code used by me is...with this code iam able to enter the text into that element but Enter key is not pressed
driver.findElement(By.xpath("//*[#id='uploading-url']/input")).sendKeys("http://www.mytabletbooksqa.com/ProductImages/test1.gif");
WebElement dropdownlists = driver.findElement(By.xpath("//*[#id='uploading-url']/input"));
Actions builder=new Actions(driver);
builder.clickAndHold(dropdownlists).sendKeys(dropdownlists, Keys.ENTER).release().build();
builder.perform();
The HTML code for that element is
<div id="uploading-url">
<p class="reduce-space"><img alt="web_image" src="/images/content/duelr/web.png"></p>
<input placeholder="Enter URLs to upload from web" type="text"></div>
Please guide me on what could possibly be going wrong here. Thank you for your time.
Following code can help:
WebElement dropdownlists = driver.findElement(By.xpath("//*[#id='uploading-url']/input"));
dropdownlists.sendKeys("http://www.mytabletbooksqa.com/ProductImages/test1.gif");
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Lemme know if this works for you!
It looks like you doing them all together. I could be a timing thing. I would recommend you separate the actions out a little bit.
Actions builder=new Actions(driver);
builder.clickAndHold(dropdownlists).perform();
builder.sendKeys(Keys.ENTER).perform();
builder.release().perform();
Then step through these and see what happens one at a time. If it works stepping through and not all at once then I would add a wait statement between the dropdown and the enter. I also removed the element part of the send keys as you already have the focus from the previous action.
While using AWT might work on a browser it will fail on the headless mode.
You could use JDollarx library for same and just write the following to trigger a key press and release event.
sendKeys(Keys.ENTER).toBrowser();
https://github.com/loyada/dollarx

How to click Image icon using selenium webdriver

I am trying to click the image icon via xPath but when i run the code the link present on image icon is not opening. could you please help me in resolving this issue.
The Code i used to click the mail icon :
driver.findElement(By.xpath("//*[#id='e-switcher-mail-icon']")).click();
As slanec said more information is required or might be the element is not loaded. If u feel that the element has loaded and still its not happening, using java script is one way to click the image element.
Something like this
WebElement element = driver.findElement (By.xpath ("//*[#id='e-switcher-mail-icon']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript ("arguments[0].click();" , element);
Could be that the element hasn't yet loaded in the DOM. Try waiting for expected conditions:
Wait<WebDriver> wait= new FluentWait<WebDriver>(driver).withTimeout(15L, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS);
WebElement icon = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='e-switcher-mail-icon']")));
icon.click();
Touché. It's the frames. Either <frame> or <iframe>, both need special care, see the documentation on the topic.
What you need to do:
driver.switchTo().frame("s_MainFrame");
after this, the driver's context will switch to the frame and all searches will be done in it, so you should be able to find the element without any further problems.
Once you're done in the frame and you need to switch back to the default context of the page, do:
driver.switchTo().defaultContent();