How to remove an element attribute using Selenium and C#? - 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("Submitted")">Click to Submit</a>

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...

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);

Related

How to click on __doPostBack enabled Update button with no ID using C# Selenium?

How to click Update button with no ID using C# Selenium?
<input type="button" value="Update" onclick="javascript:__doPostBack('ctl00$ContentPlaceHolder8$GridView1','Update$0')">
I used the code below but it does not work:
driver.FindElement(By.XPath("//*[#id='ContentPlaceHolder8_GridView1']/tbody/tr[2]/td[6]/input")).Click();
The desired element is n __doPostBack enabled element. So to Click() on the element you have to induce WebDriverWait for the ElementToBeClickable() and you can use either of the following Locator Strategies:
CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input[value='Update'][onclick*='ContentPlaceHolder']"))).Click();
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[#value='Update' and contains(#onclick, 'ContentPlaceHolder')]"))).Click();
Reference
You can find a couple of relevant detailed discussions in:
web scraping for javascript __doPostBack contain a herf in td
How do I wait for a JavaScript __doPostBack call through Selenium and WebDriver
How to paginate through the page numbers when href contains javascript:__doPostBack()

Issue locating elements with dynamic ids

I'm kinda new to selenium, and I'm stuck at figuring out one problem: I have an input that's inside this html block and my task is locate it and send keys. The problem is that not only the id is dynamic, but also css selector and Xpath
It all looks like this
Xpath //*[#id="qf_25239c53-8f51-a9af-adff-fb4c61c369dd"]
CSSselector # qf_6fc767f5-f6d9-ce40-dc78-df0e43f6dc75
<label data-v-1a920554="" for="qf_b75b7021-5df4-2283-1c79-23bddcde3a3d" class="q-field row no-wrap items-start indent q-input q-field--outlined q-field--dense">
<div class="q-field__inner relative-position col self-stretch column justify-center">
<div tabindex="-1" class="q-field__control relative-position row no-wrap">
<div class="q-field__control-container col relative-position row no-wrap q-anchor--skip">
<input tabindex="0" placeholder="Заголовок" id="qf_b75b7021-5df4-2283-1c79-23bddcde3a3d" type="text" class="q-field__native q-placeholder"></div></div></div></label>
I solved no such element exception by doing this
IWebElement MarkerNameInput = Setup.Driver.FindElement(By.XPath("(//*[#class = 'q-field__native q-placeholder'])[1]"));
But it had only fixed one exception with another and now I'm stuck at:
InvalidSelectorException: invalid selector: An invalid or illegal selector was specified
So the question is what is the correct way to locate input, so I can send keys inside of it?
The desired element is a dynamic element so to invoke SendKeys() you have to induce WebDriverWait with ExpectedConditions as ElementToBeClickable Method (By) and you can use either of the following solutions:
CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.q-field__native.q-placeholder[id^='qf_'][placeholder='Заголовок']"))).SendKeys("Vladimir Krygin");
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[#class='q-field__native q-placeholder' and starts-with(#id, 'qf_')][#placeholder='Заголовок']"))).SendKeys("Vladimir Krygin");
Try following as css selector please :
input.q-field__native.q-placeholder

How to get attribute from child node through Selenium and C#

Below mentioned is a node in a webpage and the objective is to get the data inside the attribute "onclick". I am aware that i can use GetAttribute("onclick") to get the data.
But for a reason I am only locating the td inside which this input node is present. Can someone tell if there is a way to get the attribute data "onclick" of the child 'input' node from parent 'td' node.
<td align="center">
<input type="button" class="button" value="View Pdf" onclick="showFilePreView('98374');">
</td>
If you use selenium-webdriver, then you could find the parent element first, then use childElement = parentElemnent.FindElement(By.) to find the child.
In your case, you could try the code below:
IWebElement parentElement = YourWebDriver.FindElement(By.TagName("td"));
// those codes above assume that there is only one "td" node in your case.
IWebElement childElement = parentElement.FindElement(By.Name("button"));
String theStringYouWant = childElement.GetAttribute("onclick");
Hope those code could solve your case.
The desired element is an dynamic element so to get the value of the attribute onclick i.e. showFilePreView('98374') you have to induce WebDriverWait for the element to be visible and you can use either of the following solutions:
CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("td[align='center']>input.button[value='View Pdf']"))).GetAttribute("onclick")
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//td[#align='center']/input[#class='button' and #value='View Pdf']"))).GetAttribute("onclick");
Note: Here the assumption is the <td> node is unique within the HTML DOM

How to construct an XPath/CssSelector for the dynamic button where class, ID and name are not present

below is the inspected content, Kindly help me in locating the Checkout button.
<button data-testid="continueCheckoutButton" ng-class="continueDellMetricsClass" ng-click="continueButtonClick()" ng-disabled="disableContinueButton" class="btn btn-success btn-block continueButton" data-metrics="" type="button">Checkout</button>
If the button name is getting changed dynamically, then you can use the below xpath
//button[#ng-class='continueDellMetricsClass']
Edit:
First you need to perform the page scroll down action using IJavaScriptExecutor and then need to find the element with the reference of cartsummary div.
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.scrollBy(0,1000)");
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath("//div[#ng-class='PaymentIconsEnabledCss']//button[#ng-class='continueDellMetricsClass']")));
driver.FindElement(By.XPath("//div[#ng-class='PaymentIconsEnabledCss']//button[#ng-class='continueDellMetricsClass']")).Click();
As per the HTML you have shared the element is an Angular element so you have to induce WebDriverWait for the element to be clickable as follows:
var myElement = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[#class='btn btn-success btn-block continueButton' and contains(.,'Checkout')]")));
Try below xpath:
//button[text()='Submit Order']
Code:
driver.findElement(By.xpath("//button[text()='Checkout']")).click();
Try this:
//contains[text(),'Submit Order']

C# selenium unable to locate button through xpath

I'm trying to locate an element using XPath but my code is incorrect and I'm not sure of the write syntax.
I entered the code below which isn't working.
IWebElement customizeButton = driver.FindElement(By.XPath("//*[#id='container']/div/div[1]/div[1]/div[2]/button[2]"));
The HTML code for the element is below
<button class="button u-space-ls js-customize-button button--primary " data-tooltip="{"placement":"left","title":"Customize"}" data-reactid=".0.0.0.3.3"><span class="icon icon-gear" data-reactid=".0.0.0.3.3.0"></span><span class="u-small-hidden u-medium-hidden" data-reactid=".0.0.0.3.3.1"> Customize</span></button>
Can you please let me know how I should correct my code?
Thanks
If you are looking to locate the button with text as Customize as the element is JavaScript enabled element you have to induce WebDriverWait as follows :
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement customizeButton = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[#class='button u-space-ls js-customize-button button--primary']//span[#class='u-small-hidden u-medium-hidden']")));

Categories

Resources