Here is how my html looks like on the web page:
<form id ="invoice request">
<div id = "charges">
<div id = "charges">
<div id= "billing">
<a id = "modify" class="modify_link" href = "#"> Modify </a>
I am unable to load Web page by clicking on Modify charges link. Selenium test passes without actually loading webpage.
Here are my trails:
driver.findElement (By.id ("modify")).click ();
wait.until (ExpectedConditions.presenceOfElementLocated (By.id ("modify")).click ();
What's wrong in the code?
You're expected condition doesn't actually do anything there. You're HTML shows that the element is already present when you click, because it's what you clicked. That element will still be on the DOM. You need to find an element (on what I am assuming is an AJAX call) that is presented only after you have clicked the modify button to ensure that the click button you invoked did anything.
Related
I have a Angular application which I would like to test using Selenium Web Driver with Java.
After successful or unsuccessful user login notification message is displayed into the web page:
<div class="overlay-container">
<div id="toast-container" class="toast-top-right toast-container">
<div toast-component="" class="toast-error ngx-toastr ng-trigger ng-trigger-flyInOut" style="opacity: 1;">
<!----><button aria-label="Close" class="toast-close-button ng-tns-c11-11 ng-star-inserted" style=""><span class="ng-tns-c11-11" aria-hidden="true">×</span></button><!----><!----><!---->
<div aria-live="polite" role="alertdialog" class="toast-message ng-star-inserted" aria-label="Logon failure: unknown user name or bad password." style=""> Logon failure: unknown user name or bad password. </div>
<!---->
</div>
</div>
</div>
I would like to intercept the text of the message. I tried this:
// //*[#id="toast-container"]
// //*[#id="toast-container"]/div/div
// Click Login button to submit login form
WebDriverWait failedLoginWebDriverWait = new WebDriverWait(driver, 7000);
WebElement failedLoginWebElement = failedLoginWebDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='toast-container']")));
boolean displayed = failedLoginWebElement.isDisplayed();
WebElement element = failedLoginWebElement.findElement(By.xpath("//*[#id='toast-container']/div/div"));
String text = element.getText();
System.out.println("displayed label: " + displayed);
System.out.println("text: " + text);
I get false and NPE for the second findElement.
Do you know how I can solve this issue?
isDisplayed() is returning false. This might be because you're looking (waiting) for the presence of the element in the DOM. This doesn't mean it isDisplayed(). It's a bit complicated, there is a good answer here but in summation, it's size needs to be non-zero and some atrributes must be set for it to be considered Displayed. Likely the toast is IN the DOM, but not yet fully rendered to be considered displayed.
The second issue with the NPE might be the fact you're taking the toast element and doing a findElement on it which means the xpath is going to be executed relative to the toast element. Which means the xpath is actually executing as:
//*[#id='toast-container']/.//*[#id='toast-container']/div/div
which is not what you truly want. You likely want to just do:
WebElement element = failedLoginWebElement.findElement(By.xpath("/div/div"));
Here is the scenario:
I click a button and a popup comes and I want to test when I click on button, the popup should be visible
Sample code:
<button id='bt'>
<div id ='new_div' data-state = visible >
cy.get('#bt').click()
//after clicking this I need to test data-state of "new_div" is visible/not
cy.get('#new_div').should('have.data-state','visible') //something like this
You can do this:
cy.get('[data-state = visible]').should('be.exist')
It will basically check if this tag data-state = visible present or exist inside your DOM at the moment when the popup is visible.
But the best way, of course, is to grab selector from the opened popup!
I have button in our website, and border areas of the button is not clickable. So, i need to make sure the button is getting clicked at the center. Is it possible via selenium?
I tried using coorinates, but it is not recommended for our scenario.
xpath used : //div/button[#id='clickme']
<div class="col-lg-10 col-sm-12 p-0 pt-4">
<button class="click mb-3 " tabindex="0" id="clickme">+ Click Here</button>
</div>
Java code used to click
WebElement button = driver.findElement(By.id("clickme"));
button.click();
I guess the click is happening sometimes[2 out of 10 times] on the border where it is not clickable(hand symbol not shown) . As a result report says the click action happened, but there is no event fired on the website.
To identify the element with text as Click Here you can use either of the following Locator Strategies:
cssSelector:
"button.click#clickme"
xpath:
"//button[contains(#class, 'click') and #id='clickme'][contains(., 'Click Here')]"
Fyi: I'm using the selenium package for R, the selection code is equal to javascript or python so I'm asking a general selenium question.
I have a container which I have to make visible by a click, this works.
Then I try to select an element inside this container, I think I find it correctly but the click on the element only makes the popup window disappear.
Example code:
<div class="dateRanges" style="top: 275.313px; display: block;">
<a class="top dateOption CUSTOM" id="id32" href="javascript:;">
Benutzerdefinierter Zeitraum
</a>
<a class="dateOption TODAY" id="id33" href="javascript:;">
Heute
</a>
...
</div>
Element I try to find is "top dateOption CUSTOM".
My different tries which all failed:
remDr$findElement(using = 'xpath','//a[contains(#class,"top")]')$clickElement()
remDr$findElement(value = '//a[#class = "top dateOption CUSTOM"]')$clickElement()
remDr$findElements(using = 'css selector','a[class="top dateOption CUSTOM"]')[[1]]$clickElement()
I don't get any error message, the click seems to happen as the popup disappears, but the effect of the button does not.
I tried to save the object, wait a few seconds and click afterwards with no different effect.
I also tried different approaches with "idc4" with the same outcome.
Would appreciate any help, thank you.
I am constantly getting "No Such Element Exception" for "First Name" test box
Below is my code:
public class southwestSignUpSave {
WebDriver oBrw;
#Before
public void loadwebsite (){
oBrw = new FirefoxDriver();
oBrw.manage().window().maximize();
oBrw.get("https://southwest.com");
oBrw.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void signUpAndSave(){
oBrw.findElement(By.partialLinkText("OFFERS")).click();
oBrw.findElement(By.partialLinkText("Sign")).click();
//oBrw.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebDriverWait oWait = new WebDriverWait(oBrw, 30);
oWait.until(ExpectedConditions.presenceOfElementLocated(By.id("FIRST_NAME")));
oBrw.findElement(By.id("FIRST_NAME")).clear();
oBrw.findElement(By.id("FIRST_NAME")).sendKeys("abc");
oBrw.findElement(By.id("LAST_NAME")).clear();
oBrw.findElement(By.id("LAST_NAME")).sendKeys("asd");
oBrw.findElement(By.id("EMAIL")).clear();
oBrw.findElement(By.id("EMAIL")).sendKeys("abc#asd.com");
new Select(oBrw.findElement(By.id("HOME_AIRPORT"))).selectByVisibleText("Akron/Canton, OH - CAK");
oBrw.findElement(By.id("IAN")).click();
}
}
I tried to use id and name.
where am I going wrong. I am new to Selenium WD
U can try finding the element using xpath. For that u need to install the firepath plugin in firefox and then inspect the element using firepath.
oBrw.findElement(By.xpath("copy paste the xpath here")).clear();
I would also recommend loading the driver using System property inside loadwebsite() method.
System.setProperty("webdriver.firefox.driver", "//your driver path");
oBrw=new FirefoxDriver();
if the Sign page opens in a new tab/window then u need to navigate to that tab/window because Selenium by default stays in the opening tab. To navigate u need to add the following lines of code after clicking on "Sign"-
Set<String> s=wd.getWindowHandles();
Iterator<String> it=s.iterator();
it.next();//control goes to 1st default tab
String str=it.next().toString();//control goes to the next tab
oBrw.switchTo().window(str);//driver switches to the new window/tab.
if the element is present inside a frame then also u need to switch to that frame first before finding element inside it. Below is the code-
WebElement web=oBrw.findElement(By.xpath("copy paste your frame xpath"));
oBrw.switchTo.frame(web);
now try to find the element present in the new tab/window.
FIRST NAME input text field is inside iframe. Check the below piece of HTML.
<iframe frameborder="0" src="https://luv.southwest.com/servlet/formlink/f?kOHpjQACAY" onload="scroll(0,0);" verticalscrolling="no" horizontalscrolling="no" scrolling="no" title="Click 'n Save signup form"></iframe>
<html dir="ltr">
<head>
<body>
<p>
<span class="required">*Required</span>
</p>
<div class="clear"></div>
<form id="cnsForm" onsubmit="return validateForm();" action="https://luv.southwest.com/servlet/campaignrespondent" method="POST">
<div class="form_field first_name">
<label for="first_name">
<input id="FIRST_NAME" type="text"=""="" maxlength="25" size="22" name="FIRST_NAME">
</div>
...
Hence selenium is unable to find out the element. Here we need to explicitly switch to iframe as below. Insert below code snippet before you find FIRST_NAME. (You can insert well formatted xpath of iframe. I just grabbed it from firebug.)
WebElement iframeSwitch = oBrw.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div[1]/div/div/div[4]/div/div/div/div[3]/iframe"));
oBrw.switchTo().frame(iframeSwitch);
That Text box is inside an iFrame, so you need to switch to that iFrame first then try findElement method to locate textbox.
oBrw.findElement(By.partialLinkText("OFFERS")).click();
oBrw.findElement(By.partialLinkText("Sign")).click();
oBrw.switchTo().defaultContent();
oBrw.switchTo().frame(0);
WebElement id = oBrw.findElement(By.name("FIRST_NAME"));
id.sendKeys("USERNAME");
Hope this helps.