1

I'm trying to convert some code from VBA in Excel to C# Selenium and I'm struggling with the VBA verbiage. The VBA code parses a dropdown menu on a website like this:

Set b = a.contentDocument.getElementsByTagName("frame")("iframe_tier1")
Set c = a.contentDocument.getElementsByTagName("frame")("iframe_tier2")

For Each Ele In b.contentDocument.getElementsByTagName("select")("selectDropdownItem")
    If Ele.Value = "xxxx" Then
        Ele.Selected = True
        Exit For
    End If

Next

I'm trying to understand the second argument in the getElementsByTagName (ex. ("select")("selectDropdownItem"))

Is the second item ("selectDropdownItem") a second argument? The C# doesn't seem to want to allow something like:

myDriver.FindElements(By.TagName("select", "selectDropdownItem"));

I've also tried:

IList<IWebElement> myElements = myDriver.FindElements(By.TagName("select")("selectDropdownItem"));
foreach(IWebElement ele in myElements)
{
MessageBox.Show(ele.Text);
}

Each time I get an error saying TagName cannot accept multiple arguments. What is the second item in ("") in the VBA and how I can convert that to C#?

1
  • I don't recognize that syntax and can't imagine what it might be. Rather than trying to "convert" it, let's just rewrite it. Please post the relevant HTML for the SELECT. Additionally, a link to the page itself would be helpful as well. Commented Mar 15, 2023 at 17:37

1 Answer 1

1

The code below will take care of the For Each loop in the code you provided.

SelectElement dropdown = new SelectElement(driver.FindElement(By.CssSelector("select")));
dropdown.SelectByValue("xxxx");

The part I'm unsure about is the Set b line. I'm not sure if that's supposed to switch to an IFRAME or ??? If so, you'll need to adapt the code below, providing a locator for the relevant IFRAME/FRAME tag.

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.CssSelector("frame"))); // if needed, update locator for IFRAME/FRAME

SelectElement dropdown = new SelectElement(driver.FindElement(By.CssSelector("select"))); // if needed, update locator for SELECT (not OPTION tags)
dropdown.SelectByValue("xxxx");

// once you are done with the elements inside the IFRAME/FRAME, switch back to the main page content
driver.SwitchTo().DefaultContent();

If you need help with locators, update your question with the relevant HTML for the IFRAME/FRAME and the SELECT and I'll update the code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.