简体   繁体   中英

Selenium Webdriver Java - Create button and push it

I'm using Selenium Webdriver (Chrome & Firefox) with Java .

After making all kind of actions, I came across a regular source code like this :

<input type="button" value="yoyo" class="btn" onClick="SubmitForm(this, 'XYZ','_blank')" >

and I need to push the button.. but before pushing the button (regularly), I need to change the "XYZ" to "ABC". Is there any way to do that?

Or maybe creating a new button or a form and then push it?

Or even use javascript somehow.. anything will do.

I could not find any information on how to do this, I will be thankful for your help.

You'll want to use the JavascriptExecutor as described here: https://stackoverflow.com/a/8476765/62462

WebDriver driver; // Assigned elsewhere
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('//id of element').setAttribute('onClick', 'SubmitForm(this, \'ABC\',\'_blank\')')");

You might need to find the input element via XPath if it doesn't have an id.

You can change it during runtime automation then click using JavascriptExecutor as below :-

WebElement el = driver.findElement(By.className("btn"))

((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('onClick', arguments[1]);arguments[0].click();", el, "SubmitForm(this, 'ABC','_blank')");

Note :- This will not effect as permanently solution. this effect will work on the page until the page not refresh.

Hope it will work..:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM