How to get attribute from child node through Selenium and C# - 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

Related

How to locate the element inside the <p> tags?

I have a few elements inside this kind of tag as below:
enter image description here
How do I locate these kinds of elements
I tried to locate the foresaid element, which the Selenium C# is not working
You can use xpath to locate the p element:
//div[#data-testid='patientLink']/p
Later you can call the getText() of the element located by the above selector.
give the p tag you are looking for an id. Then lookup the element using
var driver = //whatever your driver is;
driver.FindElement(By.Id("element-id-goes-here"));

How to click on the radio button through the element ID attribute using Selenium and C#

I am trying to select a radio button and input element, it has an id of group and value of In_Group. There are 4 different radio buttons with the same id but different values hence I am trying to select the correct one i am looking for.
<input class="custom-radio" id="group" name="group" type="radio" value="In_Group">
I tried something like this:
driver.FindElement(By.XPath("//*[contains(#id='group' and #value='In_Group')]"))
But the element is not found could someone help me out
To locate the element you can use either of the following Locator Strategies:
CssSelector:
driver.FindElement(By.CssSelector("input#group[value='In_Group']"));
XPath:
driver.FindElement(By.XPath("//input[#id='group' and #value='In_Group']"));
However, as it is a <input> element and possibly you will interact with it ideally you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies:
CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.custom-radio#group[value='In_Group'][name='group']"))).Click();
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[#id='group' and #value='In_Group'][#class='custom-radio' and #name='group']"))).Click();

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

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']

Categories

Resources