简体   繁体   中英

Selenium WebDriver IEDriverServer Click Link Javascript doPostBack

I'm testing an ASP.NET application that has a lot of links that call Javascript doPostBack in their href like so:

href="javascript:__doPostBack('ctl04$ctl06$Entities','Impersonate$12345')"

The Selenium WebDriver element.Click() method only works on Firefox. For IEDriverServer, nothing happens after the links are clicked.

I started Googling and it looks like the Javascript action is not actually called with the Click() method and the only suggestions I could find are to use IJavaScriptExecutor like this (this works):

var js = Globals.Driver as IJavaScriptExecutor;
js?.ExecuteScript($"javascript:__doPostBack('ctl04$ctl06$Entities','Impersonate$12345')");

This happens on IE11 on both Windows 7 and Windows 8.1. I haven't tried Windows 10 yet.

Are there any other solutions since I have a so many of these links to modify the code :( :( :(

======================================================================== Updated 01/22/17 with more screenshots and details. I currently do not have access to the actual application code, but I can request access if this doesn't suffice.

This is the HTML code of the link I'm trying to click: 在此处输入图片说明

This is my test code to find an click the link (nothing fancy):

在此处输入图片说明

As I debug through this code, I can see that the link is found and clicked successfully (no exceptions). The screen also has aa dotted line surround the said link. This code works in Firefox, so nothing is wrong with the element itself per se. However, in Firefox, after the Click() action, the screen is reloaded as expected. In IE11, nothing happens except for that dotted line.

在此处输入图片说明 在此处输入图片说明

I added the following code for IE, and after this ExcecuteScript method is called, the page is loaded with the expected result.

在此处输入图片说明

And for what it's worth, this is the resulted page that should load after that link is clicked. 在此处输入图片说明

I tried that with a small ASP.NET example and it seemed to work fine with Selenium and IE as well. Could you be more specific as to what you pgae looks like?

Here's what I tried:

WebForm1.aspx

<div>
  <asp:LinkButton runat="server" OnClick="Unnamed_Click" ID="one">test link button</asp:LinkButton>
  <asp:Label runat="server" ID="label"></asp:Label>
</div>

WebForm1.aspx.cs

protected void Unnamed_Click(object sender, EventArgs e) {
  label.Text += "Clicked";
}

Here's what the generated HTML looks like:

<a id="one" href="javascript:__doPostBack('one','')">test link button</a>

And here's the test:

driver.get("http://localhost:41058/WebForm1.aspx");

new WebDriverWait(driver, 1).until(ExpectedConditions.elementToBeClickable(By.id("one"))).click();
new WebDriverWait(driver, 1).until(ExpectedConditions.elementToBeClickable(By.id("one"))).click();
new WebDriverWait(driver, 1).until(ExpectedConditions.elementToBeClickable(By.id("one"))).click();

This works as expected with ChromeDriver and InternetExplorerDriver (using IE11 on Windows 10).

There are however some pitfalls:

The __doPostBack call will actually reload the page in the browser, so you can't use the WebElement references you might have cached before. In other words, this wouldn't work:

WebElement element = driver.findElement(By.id("one"));

element.click();
element.click();

As the second element.click() would yield a StaleElementException.

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