简体   繁体   中英

Get elements by name IE8 on different tab (Javascript)

I am developing on a program in C# that automates inputting information into a website. This program uses a library (Coypu) that allows this and for the execution of Javascript to manipulate further.

The problem comes in two forms:

  • The website only uses names (not ids) for its objects and I must use IE8 for them. Therefore I cannot use document.getElementsByName('name'); because it's not supported in earlier versions of IE.
  • I found a workaround by utilizing the form that's on the site but am having trouble using it because the site has to click a link to create a second tab for itself, leaving me helpless in grabbing the form in the second tab. Using something like document.forms[formIndex].elements['elementsName']; (although the tab appears to have focus) only gives me the forms from the main tab.

Has anyone ever fixed this problem or found a workaround in Javascript? Also, I'm not sure if jQuery can be used in my scenario as the Javascript is a string passed in to a C# method. However, it may or may not work.

If the website you are testing has access to jQuery you can use Coypu to populate specific fields in their respective forms:

eg

void PopulateInputField(int formName, string fieldName, string fieldValue)
{
    browser.ExecuteScript(string.Format("$('form[name='{0}'] > input[name='{1}']).val('{2}')", formName, fieldName, fieldIndex));
}

Note how the C# string passed to Coypu's ExecuteScript contains the jQuery.

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