Requirement: In our application, when I click on scan button, the Status will be "In Progress". I should keep clicking on "Refresh button", the "Stop" Status changes to "Time, date, year", it means the scan has completed. Below is the piece of code I written, but its not working.
public void verifyScanComplete(String networkName)
{
int i=0;
try{
//here I am searching for the string "In Progress" in the page
List<WebElement> stopStatus = driver.findElements(By.xpath("//*[text()='In Progress']"));
//this is the element stop status which will change from "In Progress" to "dd/mm/yy"
WebElement stopStatusChanged = driver .findElement(By.xpath("//*[#id='div_scan_set']/form/div/table/tbody/tr/td[text()='"+networkName+"']/../td[10]"));
while ((stopStatusChanged.getText().contains("In Progress")))
{
WebUtils.sleep(40000);
driver.navigate().refresh();
i++;
}
}
catch (Exception e) {}
}
Related
I'm trying to move to a different page (click 'my account'), and a floating advertisement appears:
advertisement
I tried to click on it, and can't find the "Close" element.
Found that it might related to frames, but still not works.
My Code:
public void clickMyAccount() {
driver.findElement(myAccountBtn).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void clickCloseAd(){
driver.switchTo().frame(driver.findElement(By.id("google_esf")));
driver.switchTo().frame(driver.findElement(By.id("aswift_9")));
driver.switchTo().frame(driver.findElement(By.id("ad_iframe")));
driver.findElement(By.id("dismiss-button")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
driver.switchTo().defaultContent();
}
Site:
https://practice.automationtesting.in/
Any idea how can I click the floating advertisement?
Tried: move between frames until able to find the Close element
Actual: still can't find this element
It is hard to handle advertisement popUp. So we can handle it in 2 ways:
downloading ad-blocker extension to your chrome and using the chrome option code.
download ad-bocker- https://chrome.google.com/webstore/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom
use chromeoption driver code:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("disable-infobars");
Notice that popUp gets disabled is do back. so we can is click on myaccount go back and then click back:
WebElement MyAccount = driver.findElement(By.xpath("//[#href='https://practice.automationtesting.in/my-account/']"));
MyAccount.click();
driver.navigate().back();
MyAccount.click();
Found the issue:
My 'wait' not always working, so the driver tries to close the ad when not displayed - still searching for a way to correct this
It's working without frame1: "google_esf", only frame2 and frame3 needed
Updated code:
public void clickMyAccount() {
System.out.println("Click My Account");
if(driver.findElement(myAccountBtn).isDisplayed()){
driver.findElement(myAccountBtn).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
} else {
System.out.println("My account button not displayed");
}
}
public void clickCloseAd(){
System.out.println("Click Close Ad");
if(driver.findElement(By.id("aswift_9")).isDisplayed()){
// driver.switchTo().frame(driver.findElement(By.id("google_esf"))); //Not in use but also a frame
driver.switchTo().frame(driver.findElement(By.id("aswift_9")));
driver.switchTo().frame(driver.findElement(By.id("ad_iframe")));
driver.findElement(By.id("dismiss-button")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
driver.switchTo().defaultContent();
} else {
System.out.println("Ad not displayed");
}
}
Thanks all!
public void rushitem(String rushi) {
try {
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT rfp_expdt_ind\n" +
"\tFROM e2e_rfpr_o.wrkflw_ap0001_rfp where rfp_case_id ='RFP-U500-3134';");
while (rs.next()) {
rushitem_sql = rs.getString(rushi);
System.out.println("Rush Indi from DB is : " + acctname_sql);
}
} catch (SQLException | NullPointerException e) {
e.printStackTrace();
}
}
public void validaterushitem() throws NullPointerException {
JavascriptExecutor js = (JavascriptExecutor) driver;
// WebElement rush1 = driver.findElement(By.xpath("(//*[local-name()='svg'])[4]"));
List<WebElement> rush = driver.findElements(By.xpath("(//*[local-name()='svg'])[4]"));
int checkLink = rush.size();
try {
if (rushitem_sql.equals("Y")) {
System.out.println("match successful for rush indicator");
WebElement rush1 = driver.findElement(By.xpath("(//*[local-name()='svg'])[4]"));
js.executeScript("arguments[0].setAttribute('style','border: 3px solid green;')", rush1);
} else System.out.println("match failed for rush indicator");
}
catch (NoSuchElementException e) {
e.printStackTrace();
} finally {
System.out.println("case is not rush item");
}
}
Inside chrome webbrowser, screen has multiple tabs where, 'svg element' icon exists. If 'rushitem_sql' = N or no record exists in Database then 'svg element' icon should not exist.
Screen layout is as follows:
chrome web browser has few tabs inside and each tab contains the element. I am navigating between each tab and verify icon exist or not.
Issue: if rushitem_sql = Y then it all works fine
but if rushitem_sql = N or no record exist in Database then tab1 works fine but navigating to tab 2 gives NULLPOINTEREXCEPTION.
Need help with this please.
I have tried many code changes but exception still there.
what am i missing?
I am unable to click on the images present in below carousel as shown in below snapshot. I tried a lot but failed though i was able to navigate to other images by clicking on the right Arrow navigation button . The Image Carousel is Present below the Featured Vehicles header Text (in website) which has navigation buttons both left and right.
Site Link : https://ryder.com/used-trucks
Below is the one of the approach that I tried ,and the code is below. Please i need your help on this at the earliest.
public void click_Image_Carousel_To_Open_ProductDetailsPage() throws InterruptedException
{
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();", driver.findElement(By.xpath("//h2[contains(text(),'FEATURED VEHICLES')]")));
WebElement ele1= null;
String image_link = null;
List<WebElement> image_Carousel_Links_list = driver.findElements(By.xpath("(//div[#class='photo']/a)"));
System.out.println("Size :"+image_Carousel_Links_list.size());
WebElement image_Carousel_Next_Btn = driver.findElement(By.xpath("//BUTTON[#class='slick-next slick-arrow'][text()='Next']"));
for(int i=1;i<image_Carousel_Links_list.size();i++)
{
System.out.println(+i+")Image links :"+driver.findElement(By.xpath("(//div[#class='photo']/a)["+i+"]")).getAttribute("href"));
System.out.println(" Element : (//div[#class='photo']/a)["+i+"]");
ele1=driver.findElement(By.xpath("(//div[#class='photo']/a)["+i+"]"));
//this for loop to rediscover the elements to avoid stale element exception
for(int k=0;k<500;k++)
{
image_Carousel_Links_list = driver.findElements(By.xpath("(//div[#class='photo']/a)"));
if(driver.findElement(By.xpath("(//div[#class='photo']/a)["+i+"]")).isDisplayed())
{
ele1=driver.findElement(By.xpath("(//div[#class='photo']/a)["+i+"]"));
break;
}
click_ImageCarousel_NextButton(image_Carousel_Next_Btn);
Thread.sleep(300);
}
if(ele1.isDisplayed())
{
ele1.click();
System.out.println(i+") Clicked on the Image present in Image Carousel :" +ele1.getAttribute("href"));
Thread.sleep(3000);
driver.navigate().back();
Thread.sleep(4000);
}
else
{
System.out.println(" Image Not found in Image Carousel");
}
}
}
private void click_ImageCarousel_NextButton(WebElement image_Carousel_Next_Btn) {
image_Carousel_Next_Btn.click();
}
#Then ("^I hover on (.+) menu and (.+) submenu$")
public void mousehover(String elementName,String subMenu) throws InterruptedException{
Actions actions = new Actions(webdriver);
WebElement menuHoverLink = webdriver.findElement(By.xpath("//a[text() = '" + elementName + "']"));
actions.moveToElement(menuHoverLink).build().perform();
Thread.sleep(2000);
actions.moveToElement(menuHoverLink).moveToElement(webdriver.findElement(By.xpath("//a[text() = '" + subMenu + "']"))).click().build().perform();
System.out.println("Sub menu "+subMenu+" Has been clicked");
}
Blockquote Hi every one. This is my code to done mouse hover event and then click sub link. But most of the time sub link click event is working. But some time which is not works. The mouse hover event has been done. But sub link click event is not triggering. Really don't know why this happen. Thanks in advance..
I would try somethin like moving to where you want to click and then just clicking. With hover menus you can get some odd behaviour from selenium about when an elemenet is and isnt clickable. Try;
actions.MoveToElement(menuHoverLink).Perform();
//wait til clickable, you are sleeping, a wait til element is both displayed and enabled would be better - can explain more if needed
action.Click().Perform();
note: I think just using peform will build the action without explicitly stating
EDIT:
I would assume that sometimes it takes longer than the 2seconds sleep time for the the element to be clickable. What exception is thrown?
Instead of thread.sleep use a wait to decide if the element can be clicked, something like:
public void WaitForElementToBeClickable(IWebElement element)
{
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
wait.Until<bool>((d) =>
{
if (IsElementDisplayed(element) && IsElementEnabled(element))
return true;
else return false;
});
}
public Boolean IsElementEnabled(IWebElement element)
{
try
{
return (element.Enabled);
}
catch (NoSuchElementException)
{
return false;
}
}
public Boolean IsElementDisplayed(IWebElement element)
{
try
{
return (element.Displayed);
}
catch (NoSuchElementException)
{
return false;
}
}
I am kinda new to HtmlUnit and am having some trouble getting a form to submit with HtmlImageInput.click(). When I call the method, nothing seems to happen, no form submission, no round trip to the server, or anything, as far as I can tell. The method returns immediately, returning the current page.
There's no Javascript event handler attached to the image input. It's just a plain old vanilla image input, nothing special going on. The input is initially set to disabled when the page is loaded, and then gets enabled as the user interacts with certain AJAXy elements within the page. But by the time I click on the input, it has already been enabled, so I don't think it's an AJAX issue.
Anybody have an idea of what is going on? Runnable source code pasted below.
Thanks,
Matthew
import java.io.*;
import java.util.*;
import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;
import org.w3c.dom.*;
public class Test {
public static void main(String args[]) {
try {
WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_7);
webClient.setThrowExceptionOnScriptError(false);
HtmlPage page = webClient.getPage("http://us.megabus.com");
System.out.println("got the page");
HtmlForm form = page.getFormByName("ctl01");
System.out.println("got the form");
HtmlSelect select = form.getSelectByName("SearchAndBuy1$ddlLeavingFrom");
select.click();
System.out.println("clicked the select");
HtmlOption option = select.getOptionByValue("13");
option.click();
System.out.println("clicked the option...going to sleep");
try { Thread.sleep(15000); } catch(InterruptedException e) {}
select = form.getSelectByName("SearchAndBuy1$ddlTravellingTo");
select.click();
System.out.println("clicked the select 2");
option = select.getOptionByValue("37");
option.click();
System.out.println("clicked the option 2...going to sleep");
try { Thread.sleep(15000); } catch(InterruptedException e) {}
HtmlImage image = (HtmlImage)page.getElementById("SearchAndBuy1_imgOutboundDate");
image.click();
System.out.println("clicked the image");
String month = "April";
String date = "09";
HtmlTable table = (HtmlTable)page.getElementById("SearchAndBuy1_calendarOutboundDate");
HtmlTableRow row = ((HtmlTable)table.getCellAt(0, 0).getChildElements().iterator().next()).getRow(0);
String monthString = row.getCell(1).getTextContent();
monthString = monthString.substring(0, monthString.indexOf(' '));
while(!monthString.equals(month)) {
row.getCell(2).getChildElements().iterator().next().click();
System.out.println("clicked to go to the next month");
try { Thread.sleep(15000); } catch(InterruptedException e) {}
table = (HtmlTable)page.getElementById("SearchAndBuy1_calendarOutboundDate");
row = ((HtmlTable)table.getCellAt(0, 0).getChildElements().iterator().next()).getRow(0);
monthString = row.getCell(1).getTextContent();
monthString = monthString.substring(0, monthString.indexOf(' '));
}
DomNodeList<HtmlElement> aList = table.getElementsByTagName("a");
for (int i = 0; i < aList.size(); i++) {
HtmlAnchor anchor = (HtmlAnchor)aList.get(i);
if (anchor.getAttribute("title").equals(DomElement.ATTRIBUTE_NOT_DEFINED) || anchor.getAttribute("title").equals(DomElement.ATTRIBUTE_VALUE_EMPTY))
throw new RuntimeException("DomElement ATTRIBUTE_NOT_DEFINED or ATTRIBUTE_VALUE_EMPTY");
if (anchor.getAttribute("title").equals(month + " " + date)) {
anchor.click();
try { Thread.sleep(15000); } catch(InterruptedException e) {}
break;
}
}
HtmlImageInput imageInput = (HtmlImageInput)page.getElementByName("SearchAndBuy1$btnSearch");
page = (HtmlPage)imageInput.click();
System.out.println("clicked search button");
} catch(FailingHttpStatusCodeException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
} catch(ElementNotFoundException e) {
e.printStackTrace();
} catch(IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}
That image is not an input field, it's just a plain old image:
<img id="SearchAndBuy1_imgOutboundDate" disabled="disabled" alt="calendar"
CausesValidation="False" src="images/icon_calendar.gif" style="border-width:0px;" />
There are no JS handlers specified there, so they must be attached elsewhere, and seems it's at the bottom of the page:
Sys.Application.add_init(function() {
$create(AjaxControlToolkit.PopupControlBehavior,
{"PopupControlID":"SearchAndBuy1_panelOutboundDate","Position":3,"dynamicServicePath":"/default.aspx","id":"SearchAndBuy1_pceImageOutboundDate"}, null, null, $get("SearchAndBuy1_imgOutboundDate"));
});
When your program clicks on the image, there is no form submit, just an AJAX call (presumably), so you're right, you don't get a new page back. But as your code proves (I just ran it with a debugger), the content of the HtmlPage has changed, since it now contains the calendar widget, which you were able to pull details from.
It can be a bit confusing knowing when you will get a net new HtmlPage back, but usually it's only when you would see a whole new page in the browser. I've never tried HtmlUnit against something like Gmail, but I suspect you might only ever deal with the one HtmlPage object, and everything takes places within it.