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);
Related
I need to click on a div icon. This is the html:
<div id="commandBarIconExcel"
tabindex="0" aria-label="Exp" role="btn"
class="commandBarItems__container"></div>
What i have tried:
driver.FindElement(By.CssSelector("div#commandBarIconExcel")).Click();
driver.FindElement(By.xpath("/html/body/div[1]/div/div/div/div[2]/main/div/div[2]")).Click();
driver.FindElement(By.Id("commandBarIconExcel")).Click();
new WebDriverWait(driver, TimeSpan.FromSeconds(100)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible((By.CssSelector("div#commandBarIconExcel"))));
new Actions(driver).Click(driver.FindElement(By.XPath("//div[#id='commandBarIconExcel']"))).Perform();
They all give unable to locate the error. Any suggestions, please?
This div opens after we select from some drop downs above it.
I was able to do it using JavaScriptExecutor
IWebElement elementa = driver.FindElement(By.CssSelector("div#commandBarIconExcel"));
IJavaScriptExecutor executor1 = (IJavaScriptExecutor)driver;
executor1.ExecuteScript("arguments[0].click()", elementa);
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.
In Chrome when I am clicking button, I am getting "unknown error:
cannot read property 'scrollleft' of null
Their is 1 Input field in that page I am able to enter value in that only Button click is not working.
This Click is working fine in Firefox. issue only in Chrome
IWebElement element = wait.Until(d => d.FindElement(locator));
wait.Until(ExpectedConditions.ElementToBeClickable(element));
element.Click();
Note:
There is no frames in this page, no scrolls.
You need to invoke Click() once the element is returned through WebDriverWait as follows:
IWebElement element = wait.Until(d => d.FindElement(locator));
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(element)).Click();
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);
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']")));