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