ArK
ArK

Reputation: 47

How to remove an element attribute using Selenium and C#?

below is the chunk of html from which i want "disabled="disabled"" deleted and close the dev tools window. iam using selenium-webdriver with c#. Thank you.

<a class="btn btn-success" href="javascript:;" id="SendRFQ" data-loading-text="<i class='fa fa-spinner fa-spin'></i> Processing..." disabled="disabled" onclick="return SubmitRequisitionData(&quot;Submitted&quot;)">Click to Submit</a>

Upvotes: 4

Views: 15395

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193338

To delete/remove the attribute and it's value of disabled="disabled" as the element is JavaScript enabled element you need to use WebDriverwait for the element to be visible and you can use either of the following solutions:

  • Using PartialLinkText:

    IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.PartialLinkText("Click to Submit")));
    ((IJavascriptExecutor)driver).ExecuteScript("arguments[0].removeAttribute('disabled')", element);
    
  • Using CssSelector:

    IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("a.btn.btn-success#SendRFQ")));
    ((IJavascriptExecutor)driver).ExecuteScript("arguments[0].removeAttribute('disabled')", element);
    
  • Using XPath:

    IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//a[@class='btn btn-success' and @id='SendRFQ']")));
    ((IJavascriptExecutor)driver).ExecuteScript("arguments[0].removeAttribute('disabled')", element);
    

Upvotes: 2

Andersson
Andersson

Reputation: 52685

Try below code to remove attribute from element

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("document.querySelector('a.btn.btn-success').removeAttribute('disabled')");

P.S. Note that real user will not modify HTML DOM to make link enabled, so if you need your script to simulate user-like behavior you should find another approach to make element enabled...

Upvotes: 4

Related Questions