Selemium can't find elements on a website - c#

I'm using C#, and I can't get the the settlementdate in a textbox from nested tables. Can you please help me?
I've tried the following:-
driver.FindElement(By.Id("settlementdate"))
driver.FindElement(By.Name("settlementdate"));
driver.FindElement(By.Name("//*[#id='settlementdate']");
driver.FindElement(By.Name("//input[#id='settlementdate']");
See below for the html code on the website

try
driver.FindElement(By.Xpath("//table/tr/td/input[#id='settlementdate']");

To locate the desired element so you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies:
CssSelector:
IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("table#headtable tr td input#settlementdate[name='settlementdate']")));
XPath:
IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//table[#id='headtable']//tr//td//input[#id='settlementdate' and #name='settlementdate']")));

Thank you for your help. I've managed to fix the issue.
The inputbox was in a frame, and I used driver.SwitchTo().Frame() to switch to the frame.

Related

Unable to locate WebElement using Selenium C#

I've been knocking my head trying to figure out why I can't locate and click this web element!
What I'm trying to do is pretty straightforward, I'm trying to click on any item of the following list: worten list of airpods
And I'm basically trying to locate AND click in some element of this kind:
It's completely impossible. Any help would be greatly appreciated.
I've already tried every type of xpath that I could remember, the only thing that comes close to working is when I copy the full xpath directly from the browser, but even then another error appears stating that the element isn't clickable...and obviously I don't want to deal with a full xpath.
examples of xpaths tried:
//a[#data-sku='7328250'] and its variations (#href instead of #data-sku, etc...)
same thing but with css selectors.
As per the snapshot provided to click on the desired element you can use the aria-label attribute of the <a> element and inducing WebDriverWait for the desired ElementToBeClickable() you can use either of the following Locator Strategies:
Using CssSelector:
IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("a[aria-label^='APPLE Airpods Pro 2ª Geração']")));
Using XPath:
IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[starts-with(#aria-label, 'APPLE Airpods Pro 2ª Geração')]")));

How to extract the summary value text $39 from the html provided through Selenium

HTML is as follows:
<h3 data-testid="cartSubTotalAmt" class="inline bottom-offset-0 pull-right ng-binding">$39.00</h3>
I need to fetch the total summary value $39.00.
Kindly help me to locate and fetch the value.
As per the HTML you have shared to extract the text of the total summary value i.e. $39.00 you need to induce WebDriverWait for the desired element to be visible and you can use either of the following solutions:
CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("h3[data-testid='cartSubTotalAmt']"))).GetAttribute("innerHTML");
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//h3[#data-testid='cartSubTotalAmt']"))).GetAttribute("innerHTML");

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']")));

select radio button in selenium c#

I'm new to XPath and CssSelector.
below is the target html source.
<input value="1" name="uji.model.611876.button" type="radio"></input>
611876 is a random number.
I tried with the code:
driver.FindElement(By.Id("//input[#value=\"1\"]")).Click();
and
driver.FindElement(By.Id("//input[#value='1']")).Click();
but the Unable to locate element error occurred.
I need help for that situation. Thank you for reading.
If you get ElementNotVisibleException try to wait some time until target input become visible:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//input[starts-with(#name, \"uji.model.\")][#type=\"radio\"]")));
element.Click();
You can do something like
driver.findElement(By.tagName("input")).Click();
You can try JavascriptExecuter to execute javascript code if it is causing problem -
IWebElement element= driver.FindElement(By.XPath("//input[#value=\"1\"]")));
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("arguments[0].click();", element);

How to locate an element using whole html(innerHtml) content

How to locate an element using whole html content such as Download
Please help.
use the below as cssSelector:
a[href*='view_download_report(document.MForm']
it will give u this element.
you can also use xpath:
.//a[#href='javascript:view_download_report(document.MForm,
'mm_tt_uu_999','~','D')']

Categories

Resources