Element not found in dropdown menu - selenium-webdriver

I have tried all selectors even action builder. But nothing seems to be working.
I am trying to run it on chrome
I get No Such Element Exception
Load Baubleabar
Add any item
Click on Shopping cart
Click on View Shopping Bag ( from Drop down)
Thanks...
Here is the HTML
<li class="_JS_UserLoggedOut header_subNav_listItem" style="display:inline- block">
<li class="cartDropdown_container _JS_cartWidget header_subNav_listItem">
<a class="header_subNav_link header_subNav_cartIcon" href="/checkout/cart/">
<span class="cart-qty-indicator _JS_cartQty">1</span>
<div class="cartDropdown __showCartWidget">
<button class="dropdown-cart-scrollup btn_reset _JS_crt_up" style="display: none;">
<ul class="_JS_scrollUI cart-item-container _JS_cartItemsContainer" data-item-count="" data-scrollable="false">
<div class="cartDropdown_emptyMsg">
<button class="dropdown-cart-scrolldown btn_reset _JS_crt_down" style="display: none;">
<div class="cartDropdown_subtotal group">
<a class="btn_highlight cartDropdown_viewCartLink" href="/checkout/cart/">
</div>
</li>

Please, can you show us the html code that you want to select and your webdriver code? Without these it is hard to know the problem's reason.
Using Selenium in C#, there is a method that can help you: it will match the value, name or id of the option
public void SelectIn(By by, string value)
{
var dropDownListBox = Driver.FindElement(by);
var clickThis = new SelectElement(dropDownListBox);
clickThis.Options.First(o =>
o.Text.ToLower() == value.ToLower()
|| o.GetAttribute("value").ToLower() == value.ToLower()
|| o.GetAttribute("id").ToLower() == value.ToLower())
.Click();
}

try the below code:
Thread.sleep(2000);//don't use this, use explicit wait.
//click on shopping bag at top
driver.findElement(By.cssSelector(".header_subNav_link.header_subNav_cartIcon")).click();
Thread.sleep(2000);
//if u want to get total number of items from here, use it
List<WebElement> elem = driver.findElements(By.cssSelector("#cart-items>li.cart-item"));
System.out.println("total product in ur bag is "+elem.size());
// click on view shopping bag.
driver.findElement(By.cssSelector(".shopping-cart-icon-small")).click();
Thread.sleep(2000);
//get total number of product from here
List<WebElement> elems = driver.findElements(By.cssSelector("article.cart_checkoutReview_item"));
System.out.println("total product in ur bag is "+elems.size());
let me know what happens.
U can use javascript for click
WebElement element = driver.findElement(By.cssSelector(".header_subNav_link.header_subNav_cartIcon"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

Related

how can i release a key after using action class with Sendkeys("15/16")

I am testing a drop-down where I am using an action class to select a season as "16/17" but as soon as I SendKeys it enter the correct value once and after loosing the control changes back to the defaulted season "18/19" ..please advise how should I release the key.
Note: arrow down key doesn't support here for this drop-down
using Firefox+Java+ webdriver
Actions builder1 = new Actions(driver);
WebElement mainelement = driver.findElement(By.xpath("//gc-dropdown//div[*
[contains(text(), 'Season')]]"));
Action SeriesOfActions6=builder1.moveToElement(mainelement).click()
.pause(2000).sendKeys("16/17",Keys.DOWN).click().build();
driver.manage().timeouts().implicitlyWait(30 , TimeUnit.SECONDS);
SeriesOfActions6.perform();
dropdown for "18/19" element
<div class="dropdown-display" ng-click="vm.onDropdownClick()" ng-class="{ 'clicked': vm.isDropdownOpen, 'dropdown-mobile' : vm.isMobile}">
<div class="animation ng-binding ng-scope" ng-if="!vm.isFocussed"> 18/19 </div>
dropdown for "16/17" element <div class="dropdown-item ng-scope" ng-repeat="item in vm.values track by $index" ng-click="vm.onItemClick(item)" ng-class="{ 'selected': item.isSelected}" ng-disabled="item.isDisabled && item !== vm.selectedItem"> <span class="ng-binding">16/17</span>
screenshot of a page

Unable to click play button coming on mouseover of WebElement in a WebElement List- using serenity and webdriver

I got a list of webelement using xpath = ".//div[#id='list']/ul/li"
now li tag has child tags
<div class="abc-box " tabindex="-1">
<div class="abc-img b-loaded" tabindex="-1" style="background-image: url("/binaries/abc.jpg");">
<a class="game-img__link" data-gameid="1569" tabindex="-1" title="abc" href="/1816/launchSocialGame.do?launch=real" rel="nofollow">abc</a>
</div>
<div class="abc-overlay" tabindex="-1">
<a class="abc-overlay__link" data-gameid="1569" href="/1816/launch.do?launch=real" tabindex="-1" rel="nofollow">Play</a>
</div>
</div>
I am successfully getting <a> inside first <div>/<div> to verify the title="abc". Now if title=abc, I need to click second<a> as on mouseover/click on the webelement(image) of the list, play button get overlayed.
code i am using
public void click(String gameName) {
Webelement list=getDriver().findElement(xpath = ".//div[#id='list']/ul/li");
Iterator var2 = this.list.iterator();
while(var2.hasNext()) {
WebElement we = (WebElement)var2.next();
WebElement gameImage= we.findElement(By.tagName("a"));
if (gameImage.getAttribute("title").toLowerCase().contains(gameName.toLowerCase())) {
withTimeoutOf(SECONDS_TO_WAIT, TimeUnit.SECONDS);
(new WebElement playLink = we.findElement(By.className("abc-overlay__link")); withTimeoutOf(SECONDS_TO_WAIT, TimeUnit.SECONDS).waitFor(ExpectedConditions.visibilityOf(playLink));
playLink.sendKeys(Keys.RETURN);
clickedGame=true;
break;
}else{
clickedGame=false;
}
}
}
With above code, I am getting exception of timeout on
withTimeoutOf(SECONDS_TO_WAIT, TimeUnit.SECONDS).waitFor(ExpectedConditions.visibilityOf(playLink));
Tried many thing to get correct locator of play button but all attempts unsuccessful. I am not able to understand where am I doing wrong.
Please suggest how to click on play button.
Thanks

Query over selenium automation

I am doing automation but unable to click on a hyperlink by using findElement(). Sharing that part of the inspect elment of the code . Can anyone help ?
<div class="nametag" data-request-type="immigration">
</div>
<div class="name">
</div>
<div class="info">
Click <a href="" ng-click="openNewRequest(currentUserInfo.personId, 2)">
here</a> to submit new visa request.
Considering your element code, you can use linktext to click on hyperlink. Use the below generic function to click. Following code is in c#:
click(By.LinkText("here"));
public void click(By by) //method starts from here
{
try
{
WebDriverwait wait;
wait=new WebDriverWait(driver,20);
wait.until(ExpectedConditions.elementToBeClickable(by)).click();
}
catch(WebDriverException er)
{
}
}
you can do this:
WebElement e = driver.findElement(By.cssSelector(".info a"));
String href = e.getAttribute("href");
driver.get(href); or driver.navigate().to(href);
Try Using css selector. Here's the full code.
By linkLocator = By.cssSelector("[data-request-type="immigration"] .info a");
driver.findElement(linkLocator).click();
Use explicit wait before click. you can use below xpath for click on hyperlink.
driver.findElement(By.xpath("//a[text() = 'here']"));
For how to use explicit wait please go to below link.
https://trickyautomationworld.blogspot.in/2018/02/implicit-wait-vs-explicit-wait-vs.html

Not able to click on hidden element in protractor?(Please go through the image.)

Here is the HTML code :
<li class="subdropdown">
Create Position
<ul class="list-unstyled dropdown-submenu" role="menu">
<li style="cursor: pointer;">
<a ng-click="openPositionModal($event)"><i class="glyphicon glyphicon-list-alt"></i> New Position</a>
</li>
Here is my test case:
1) Move the cursor on "Create position" toggle menu.
2) After the mouse hover, click on the "New position" menu list.
I used the below code to click on an hidden element. May be it might help someone.
Import statement:
import { browser, by, element } from 'protractor';
Code:
const hiddenElement = element(by.id('hiddenIcon'));
browser.driver.executeScript('arguments[0].click();', hiddenElement.getWebElement());
Just change the hiddenIcon to the id of your element.
browser.actions(), "by link text" and the "by partial link text" locators should help here:
var EC = protractor.ExpectedConditions;
// open up the menu
// choose position
var choosePosition = element(by.linkText('Create Position'));
browser.actions().mouseMove(choosePosition).perform();
// choose new position
var newPosition = $('a[ng-click*=openPositionModal]');
browser.wait(EC.elementToBeClickable(newPosition), 3000);
newPosition.click();

How to select element from options populated

I need to write a script with Selenium-Webdriver. I am stuck in a situation where I need to enter text (e.g : "tt") in the textbox and a list gets populated (hidden values). We need to select an option from the populated list. (Like we do in "Google" search).
<div class="select2-search">
<input type="text" autocomplete="off" class="select2-input tabindex="-1" style>
</div>
<ul class="select2-results">
<li class="select2-results-dept-0 select2-result select2-result-selectable select2-new">
<div class="select2-result-label">
<span class="select2-match">et</span>
</div>
</li>
<li class="select2-results-dept-0 select2-result select2-result-selectable select2-highlighted">
<div class="select2-result-label">"Secr"
<span class="select2-match">et</span>"ary"
</div>
</li>
<ul>
So i'll write a little code to help you.
I'm using www.google.com as exemple.
WebElement inputElement = driver.findElement(By.id("gbqfq")); // get the input
inputElement.sendKeys("zyzz"); // you want to search him on internet ;)
// get all suggestions in a list
List<WebElement> suggestionList = driver.findElements(By.CssSelector(".gsq_a table tbody tr td:first-of-type span"));
To select an option you have 2 choices, first :
// you get a random option and click on it
suggestionList.get(1).click();
or
// you only want to click on the link that contains that.
for(WebElement elem: suggestionList)
{
String text = elem.getText();
if(text.equals("zyzz motivation"))
elem.click();
}

Resources