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
Related
driver = webdriver.PhantomJS()
driver.get(url)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
driver.find_element_by_css_selector('a.btn_more').click()
I am able to click the get more link using above code
get more
but how to click on followers link?
<div class="my_show__info">
<a class="my_show__link j_get_follow" href="javascript:;" data-follow="followers"> 90</a>
<a class="my_show__link j_get_follow" href="javascript:;" data-follow="following"> 33</a>
</div>
you can form a xpath and click on the link as mentioned below. if you want using CSS. convert the following xpath with css.
driver.findElement(By.Xpath("//div[#class='my_show__info']/a[#data-follow="followers"])).click()
I am not good with CSS but I guess following should work.
driver.find_element_by_css_selector('div[class='my_show__info']>a[data-follow='followers']').click()
I would like to understand how to do the following, they asked me to try and create a function in the controller instead of writing the code in the view.
The code is:
<div class="row">
<div class="col-xs-12 max300"
uib-dropdown is-open="vm.descriptionDropdownOpen">
<textarea name="description" class="form-control"
ng-model="vm.presence.description"
ng-click="vm.toggleDescriptionDropdown()"
autofocus>
</textarea>
<ul id="descriptionDropdown" uib-dropdown-menu>
<li ng-repeat="descr in vm.loadedDescriptions"
ng-click="vm.presence.description = descr.text;
vm.descriptionDropdownOpen = false;">
{{descr.text}}
</li>
</ul>
</div>
</div>
so basically this creates a text box, and when you click on it, a dropdown menu will appear, and if you click on a string of the dropdown menu, that string will be put in the text box.
What I need to do is to create a function that will be put in the controller, so we can just call that function in the view and keep the code nicer.
This function just needs to do the last part of the code I posted above, take the string I click on from the dropdown menu and put it in the text box!
It's really simple but as I'm learning I'm not that sure on how I should write it
setDescription(text: string) {
// code should go here.
}
Sorry for this stupid question, just wanna be sure to understand correctly what I am doing! thank you
html
ng-click="vm.submitString(descr.text)"
controller
vm.submitString = function(text){
return vm.presence.description = text;
}
resolved like this:
setDescription(text: string) {
return this.presence.description = text;
}
My scenario is - after login, dashboard page is displaying ,there I want to click on a link. which will give a javascript popup. Though my xpath is correct it is not clicking on it. And the test is also showing pass.
I have attached both html and selenium code -
Html :
<!-- create connection section -->
<section class="wrangler-create-project left-align">
<div class="container">
<a class="btn z-depth-0" data-target="create-project-modal"><i class="fa fa-plus-circle left" aria-hidden="true"></i>New project</a>
</div>
</section>
Selenium Code :
#Test
public void clickNewProject() throws TimeoutException
{
UtilsMethods.login();
WebDriverWait wait = new WebDriverWait(UtilsMethods.driver, 40);
System.out.println(UtilsMethods.driver.getPageSource());
wait.until(ExpectedConditions.presenceOfElementLocated (By.xpath("//a[#data-target='create-project-modal']")));
UtilsMethods.driver.findElement(By.xpath("//a[#data-target='create-project-modal']")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[#id='project_name']")));
}
we can keyboard action events,
UtilsMethods.driver.findElement(By.xpath("//a[#data-target='create-project-modal']")).sendKeys(Keys.Enter)
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
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);