I try to send Keys to an input field via Java Selenium. I get the NoSuchElementException every time. I also tried everything from this Solution: NoSuchElementExeption, selenium unable to locate element. Thanks in Advance!
driver.findElement(By.xpath("//input[#class='pull-left ng-pristine ng-validng-empty ng-touched']")).sendKeys(t + Keys.ENTER);
<input class="pull-left ng-pristine ng-valid ng-empty ng-touched" ng-model="TagInputCtrl.tagInput" uib-typeahead="tagSuggestion for tagSuggestion in TagInputCtrl.getTagSuggestions($viewValue)" select-on-comma="" select-on-whitespace="" select-on-blur="" typeahead-focus-first="false" tag-select="TagInputCtrl.onEnter" tag-select-model="ngModel" sprd-max-input-length="50" ng-show="ngModel.length < TagInputCtrl.validatorOptions.tags.max" ng-focus="TagInputCtrl.focused = true" ng-blur="TagInputCtrl.focused = false" aria-autocomplete="list" aria-expanded="false" aria-owns="typeahead-4377-3960" style="" type="text"/>
To send a character sequence as the desired element is an Angular element so you need to induce WebDriverWait for the element to be clickable and you can use either of the following solution:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.pull-left.ng-pristine.ng-valid.ng-empty.ng-touched[ng-model^='TagInputCtrl']"))).sendKeys("Tim");
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='pull-left ng-pristine ng-valid ng-empty ng-touched'][contains(#ng-model,'TagInputCtrl')]"))).sendKeys("Tim");
There can be two things for NoSuchElementException :
Your locator is not correct.
If this is the case then you can try with this xpath :
//input[contains(#class,'pull-left ng-pristine ng-valid ng-empty ng-touched') and #ng-model='TagInputCtrl.tagInput' and #ng-focus='TagInputCtrl.focused = true']
Your webpage is integrated with Angular. So the provided xpath should work.
Your input tag might be in iframe/frame/frameset.
If this is the case, then I would recommend you to switch the focus of your driver to the particular iframe, to interact with desire element.
for switching you can try this code :
driver.switchTo().frame(name_or_id)
Generally, iframe tag contains name or id attribute, in case if both of them aren't available , you can go ahead with
driver.switchTo().frame(index)
or
driver.switchTo().frame(iframe_element)
Here, iframe_element is a web element.
Hope this helps.
Related
I'm trying to identify the element. I want to identify the "address your concerns.". I am able to reach up to div tag but i need to reach the text in the div tag.
HTML:
<span class="Linkify">
<div>
<div>
address your concerns.
</div>
</div>
</span>
I am trying to identify the text "address your concerns" so can anyone help me with this in chrome browser, I am trying?
To identify the element using Selenium you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
cssSelector:
WebElement cssElement = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("span.Linkify > div > div")));
xpath:
WebElement xpathElement = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[#class='Linkify']/div/div[contains(., 'address your concerns')]")));
Try doing it using XPath to locate your div element, something like this "//span[#class='Linkify']/div/div" and then use getText() method on this div to get your text.
Unable to locate the element using id/name/xpath/CSSSelector
Tried the below codes and both failed to give response
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\'form\']/p/button/span")));
driver.findElement(By.xpath("//*[#id=\'form\']/p/button/span")).click();
and
WebElement checkout = driver.findElement(By.xpath("//[#id=\'form\']/p/button/span"));
checkout.click();
HTML
<button type="submit" name="processCarrier" class="button btn btn-default standard-checkout button-medium" style="">
<span> Proceed to checkout <i class="icon-chevron-right right"></i> </span>
</button>
Try Following CSS Selector.
WebElement checkout = driver.findElement(By.cssSelector("button.standard-checkout span"));
checkout .click();
Or yon can use WebDriverWait and Css Selector.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.standard-checkout span")));
element.click()
Probably you are getting org.openqa.selenium.InvalidSelectorException because you should use * after // to match any node(tag) who has id=form or the specific tag name.
change it to //*[#id='form']/p/button/span
Or use more specific like
xpath : //button[#name='processCarrier'] equivalent CSS : button[name='processCarrier']
And use implicit/explicit wait to make element available in DOM to perform actions.
Presumably you will invoke click() on the <button> element, so you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.standard-checkout.button-medium[name='processCarrier']>span"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='button btn btn-default standard-checkout button-medium' and #name='processCarrier']/span"))).click();
I'm having difficulties in locating a web element in seleniumn the html is as follows:
<div class="d2l-fieldgroup">
<div class=" vui-field-row">
<span class="d2l-field vui-label d2l-offscreen">Do not ask me again for this application</span>
<label class="d2l-checkbox-container">
<input name="dontAsk" value="0" type="hidden">
<input id="dontAsk" class="d2l-checkbox vui-input vui-outline" type="checkbox">
Do not ask me again for this application
</label>
</div>
</div>
I need to click on the checkbox or text but I just cant seem to be able to do this as I always get an error back saying:
Unable to locate element: {"method":"class name","selector"
I tried this:
WebElement do_not_ask_resourcebank = driver.findElement(By.className("d2l-fieldgroup"));
do_not_ask_resourcebank.click();
But get the following error:
no such element: Unable to locate element: {"method":"class name","selector":"d2l-fieldgroup"}
I also tried replacing d2l-fieldgroup with vui-field-row and d2l-checkbox-container
I also tried:
WebElement do_not_ask_resourcebank = driver.findElement(By.cssSelector("html/body/form/div/div[3]/div/label"));
and
WebElement do_not_ask_resourcebank = driver.findElement(By.xpath("id('confirmForm')/x:div/x:div[3]/x:div/x:label"));
but I just cannot seem to click on this element, very frustrating for a newbie like myself. Can someone point me to where I am going wrong?
If you are getting NoSuchElementException as your provided exception, There may be following reasons :-
May be you are locating with incorrect locator, you're locating actually <div> element with class name which looks like dynamically generated class name instead of actual <input type = 'checkbox'> element which you want. And other also looks unstable, You should try to locate desire element using By.id() as :-
WebElement do_not_ask_resourcebank = driver.findElement(By.id("dontAsk"));
May be when you are going to find element, it would not be present on the DOM, So you should implement WebDriverWait to wait until element visible as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dontAsk")));
May be this element is inside any frame or iframe. If it is, you need to switch that frame or iframe before finding the element as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
//Find frame or iframe and switch
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name"));
//Now find the element
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dontAsk")));
//Once all your stuff done with this frame need to switch back to default
driver.switchTo().defaultContent();
Edited :
I havent tried the iframe yet because I am not sure what I am doing I havent used this tool much. The html code for the iframe is : <iframe id="d2l_1_69_954" class="d2l-iframe d2l-iframe-fullscreen d2l_1_67_746" name="d2l_1_69_954" src="/d2l/lms/remoteplugins/lti/launchLti.d2l?ou=6606&pluginId=f8acc58b-c6d5-42df-99d9-e334f825c011&d2l_body_type=3" title="TALLPOD" scrolling="auto" allowfullscreen="" onload="this.m_loaded=true;" style="height: 314px;" frameborder="0"> how do you locate this iframe using your example above?
Looks like iframe id and name both are dynamically generated which could be change every time when you locate, you should try to locate this iframe with unique locator, I think it's title name is unique and fixed so you should try to locate this iframe before locating desired element as :-
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title='TAFEPOC']"));
I am trying to code in java using selenium webdriver to click on Dropdown list in walmart page. But I am unable to access the li elements.
<button class="js-flyout-toggle dropdown" aria-haspopup="true" type="button" data-cat-id="0" aria-expanded="false"> All </button>
<div class="js-flyout-modal flyout-modal">
<ul class="block-list">
<li><button class="no-margin font-semibold" type="button" data-cat-id="0" tabindex="-1"> All Departments </button></li>
<li><button class="no-margin font-semibold" type="button" data-cat-id="91083" tabindex="-1"> Auto & Tires </button></li>
<li><button class="no-margin font-semibold" type="button" data-cat-id="5427" tabindex="-1"> Baby </button></li>
I wanted to access Baby using selenium webdriver in Java.
Below is my code:
driver.get("http://www.walmart.com");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement dropdown = driver.findElement(By.xpath("html/body/div[2]/header/div[3]/div/div/div/div/div[3]/form/div/div[1]/div/button"));
dropdown.click();
List<WebElement> liElements = driver.findElements(By.xpath("//*[#class='block-list']/li"));
for ( WebElement we: liElements) {
System.out.println(we.getText());
}
But this gives an error as below:-
"Exception in thread "main"
org.openqa.selenium.ElementNotVisibleException: Element is not
currently visible and so may not be interacted with Command duration
or timeout: 132 milliseconds".
Please help
You defined WebDriverWait but you don't use it
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement dropdown = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("dropdown")));
Implicit wait will wait for element to exist in the DOM. If you want to use explicit wait you need to use Expected Conditions as well. Only defining WebDriverWait doesn't actually do anything.
Your code looks fine. Please try below xpath :-
//div[#class='js-flyout-modal flyout-modal']//ul[#class='block-list']/li
Now first try to put thread.sleep if it works then it is problem of wait only
Thread.sleep(30000);
But then don't use Thread for your script as it's not recommended .. It's just to ensure that script is failing because of time
Hope it will help you :)
May be other element in walmart page match your xpath //*[#class='block-list']/li and that element is not visible.
Do you know how to find and click at an element like this:
<div class="offer ctrlOpenOfferInfo">
<a class="linkOffer" href="offer_view.html?id=1007"/>
<p class="photo">
<p class="name">New offer</p>
<p class="price">123</p>
<div class="rate ctrlViewRateOffer" data-value="0.0000">
<p class="date"/>
<div class="hide info">
<div class="bgInfo"/>
using Selenium WebDriver and using the name of the element as there are few very similiar elements on the page?
Here's what I've tired so far:
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[text()='New offer']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[#id='formDevelopmentElementAdd'][text()='New offer']")));
driver.findElement(By.xpath(".//*[#id='formDevelopmentElementAdd']/div/p[2][text()='New offer']")).click();
or:
driver.findElement(By.xpath("//p[#class='name'][text()='New offer']")).click();
or:
String address = driver.findElement(By.xpath("//*[contains(text(), 'New offer') and #class='name']")).getAttribute("href");
driver.get(baseUrl + address);
or:
driver.findElement(By.xpath("//a[Text()='New offer']")).click();
I've tried these with text, class, xpath...
Assuming you want to click the p tag containing New offer:
I would think this element is not inside an iframe. If so, you must use driver.switchTo().frame("name or xpath or css for frame locator"); first before start looking for element.
Now, how can you find that element. Consider the following options:
Using xpath text based search
//p[.='New offer']
//to be more precise you can do
//p[#classs='name'][.='New offer']
You can use By.className('name') to identify that element even though that name does not look like unique to me
Using cssSelector
.name
using nth-child() function of cssSelector
.offer.ctrlOpenOfferInfo>a>p:nth-child(2)