0

I wrote in Python a script which uses Selenium to auto-complete a form. It works with no issues.

I am very new to C# but I thought I would try and port it over so I can build a Windows executable to share it with a couple of non tech-savvy family members.

However, when I try what appears to be the same code, I get a timeout in C#.

As an example, I am trying to click a radio button:

HTML of radio button:

<input data-v-7af3e24c="" type="radio" id="condition-2" name="condition" class="govuk-radios__input" value="false">

Python (this works):

WebDriverWait(driver, max_wait).until(EC.presence_of_element_located(
    (By.CSS_SELECTOR, '[id$=condition-2]'))).click()

However, when I try what I think is the same request in C#:

int elementLoadTime = 5; // Max 5 seconds for element to load 
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, elementLoadTime)); 
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.CssSelector("[id$=condition-2]"))).Click();

This produces:

Exception thrown: 'OpenQA.Selenium.WebDriverTimeoutException' in WebDriver.dll

The strange thing is, I am able to select the element in C# using the full XPath, so the element is clearly loading, and it strongly suggests the problem is with my CSS selector query.

// This works
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("/html/body/div[1]/div[2]/main/div/div/form/fieldset/div[2]/div/div/div[2]/label"))).Click();

However, obviously that is a very brittle way of writing the code which will break the moment the site changes slightly.

I have also tried "[id$='condition-2']" and "[id$=\"condition-2\"]", to no avail.

Can anyone shed any light on what I am doing wrong?

3
  • Try removing the $ so it's just id= Commented Jan 9, 2022 at 10:55
  • Thanks for response - I tried but I still get the same WebDriverTimeoutException error. Commented Jan 9, 2022 at 11:15
  • So in case anyone finds this question in 5 years when googling - I could not fix the issue. Ultimately, I circumvented it by creating an object which finds all input elements (var allInput = driver.FindElements(By.XPath("//input"));) putting some logic in the C# code rather than the Xpath: foreach (var input in allInput) { if (input.GetDomProperty("value") == ("false")) { input.Click(); } } Commented Jan 10, 2022 at 11:02

1 Answer 1

1

Instead of using:

By.CssSelector("[id$=condition-2]")

Just use:

By.Id("condition-2")

If you want to use Xpath instead, do two things. First, forget that there's an option in your browser to 'Copy Xpath' - just purge it from your mind. Second, use this Xpath instead:

By.Xpath("//input[@id=\"condition-2\"]")
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response - think that's a good tip about ignoring Copy XPath, which is what I ultimately ended up doing. I did try By.Id() but that couldn't find the element in C# either, even though it did in Python. It was all a bit confusing. I got it working in the end by just finding all the input elements and then searching their text - I don't know it didn't work the other way though.

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.