I Have Divided The Xpath into 4 Parts Because in Xpath only one Row Value Changes That is /tr[2] Like that 3,4,5 upto the Path Ends.
public class NewTest {
#Test
public void f() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "F:\\New folder\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://xxxxxxxxxxx/index.aspx");
driver.manage().window().maximize();
WebElement ee= driver.findElement(By.id("ddlstore"));
Select s11 = new Select(ee);
s11.selectByVisibleText("xxxxxx");
String s1 = "/html[1]/body[1]/form[1]/div[5]/center[1]";
String s2 = "/div[1]/table[1]/tbody[1]/tr[";
String s3 ="]";
String s4 = "/td[11]/input[1]";
for(int i=2;i<=99;i++)
{
String finalXpath= s1+s2+i+s3+s4;
driver.findElement(By.xpath(finalXpath)).click();
Thread.sleep(3000);
try {
WebElement e1 = driver.findElement(By
.xpath("//p[#id='skipcount']"));
System.out.println(e1.getText());
WebElement e2 = driver.findElement(By.xpath("/html/body/div[1]/div/div"));
WebElement e3 = driver.findElement(By.xpath("/html/body/div[1]/div/div/div[3]/button[2]"));
Actions a1 = new Actions(driver);
a1.moveToElement(e2).click(e3).build().perform();
} catch (Exception e) {
System.out.println("Can't Click on The Element ");
}
}
The Element is Not Clicking it is Showing an Error , Unable to Locate Element,Xpath
Html Code For The First Xpath Divided into String :
<input type="submit" name="CustomPaging_GridView$ctl02$ff" value="SKIP" onclick="product_skip(this);" id="CustomPaging_GridView_ff_0" class="button2" data-id="12691247570" data-id1="36025">
Html Code For Xpath Which is in Try/Catch Block:
<div class="modal-footer">
<span id="prcid" style="display:none;">processing...</span>
<button type="button" id="skipok" onclick="skipoverall(this)" class="btn btn-primary" data-id="12691247570" data-id1="36025">Ok</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
</div>
Ps : Actually The Doubt is, I Need To Click an Skip Button Which has an Same Xpath For All Button and Each Time When i Click Skip Button an Popup Will Appear in That i Need To Click The Cancel Button
Try this :
int i = 1;
String s2 = "/div[1]/table[1]/tbody[1]/tr[' " +i+ " '];
There is no need of String s3.
Try this:
// you don't need 'e2', instead you hover 'e3' and click on it
WebElement e3 = driver.findElement(By.xpath("/html/body/div[1]/div/div/div[3]/button[2]"));
Actions a1 = new Actions(driver);
a1.moveToElement(e3).click(e3).build().perform();
Suggestion:
String s1 = "/html[1]/body[1]/form[1]/div[5]/center[1]";
String s2 = "/div[1]/table[1]/tbody[1]/tr[";
String s3 ="]";
String s4 = "/td[11]/input[1]";
for(int i=2;i<=99;i++)
{
String finalXpath= s1+s2+i+s3+s4;
can be replaced with:
for(int i=2;i<=99;i++)
{
String finalXpath= "/html[1]/body[1]/form[1]/div[5]/center[1]/div[1]/table[1]/tbody[1]/tr[" + i + "]/td[11]/input[1]";
...
Related
I've got a page (Angular8) with multiple button/input elements having the same ID.
The ID's are, in the order the appear on the page:
1: for="lastOppDokument-0-VERGE-LEGITIMASJON"
2: for="lastOppDokument-1-VERGE-VERGEMAL"
...
3: for="lastOppDokument-0-VERGE-LEGITIMASJON"
4: for="lastOppDokument-1-VERGE-VERGEMAL"
The elements on lines 1,3 and 2,4 have the same ID.
I'm trying to access the elements using XPath and index like this:
driver.findElement(By.xpath("(//input[#for='lastOppDokument-0-VERGE-LEGITIMASJON'])[1]
driver.findElement(By.xpath("(//input[#for='lastOppDokument-0-VERGE-LEGITIMASJON'])[2]
If I check the size:
int x = driver.findElements(By.xpath("(//input[#for='lastOppDokument-0-VERGE-LEGITIMASJON'])")).size();
it says "2", which is correct.
However, when I try to click those two buttons, the first one (LINE 1) is clicked, but then the next button on the page (LINE 2) i clicked istead of the one on LINE 3.
There's obviously something wrong with my XPath expression, but what? Also, the page is an Angular page, and in the markup code "for" is used to automatically index the ID. But selenium finds the elements (albeit not all the correct ones) using ID. But maybe I need to use something else to identify the correct elements?
UPDATE:
To troubleshoot, I'm skipping the use of a loop and just trying to access the two elements manually:
WebElement buttonToClick = driver.findElement(By.xpath("(//input[#id='lastOppDokument-0-VERGE-LEGITIMASJON'])[1]"));
filUtils.uploadFile(buttonToClick, legitimasjonfil);
WebElement buttonToClick2 = driver.findElement(By.xpath("(//input[#for='lastOppDokument-0-VERGE-LEGITIMASJON'])[2]"));
filUtils.uploadFile(buttonToClick2, legitimasjonfil);
The uploadFile (and related) methods:
public void uploadFile(WebElement id, String filename) {
id.sendKeys(getAbsolutePathToTestFile(TESTFILER_PATH + "/" + filename));
}
private String getAbsolutePathToTestFile(String path) {
return copyFileToTargetTempPath(path, path);
}
private String copyFileToTargetTempPath(String originPath, String destinationPath) {
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(originPath);
File destination = new File(TARGET_TEMP_PATH + destinationPath);
try {
assert resourceAsStream != null;
org.apache.commons.io.FileUtils.copyInputStreamToFile(resourceAsStream, destination);
} catch (IOException e) {
throw new RuntimeException(e);
}
return destination.getAbsolutePath();
}
UPDATE 2:
<td>
<span class="hb-felt knappesamling-gruppe hb-avstandIngen ng-star-inserted">
<input class="hb-lastOppFil-input hb-bare-skjermleser" type="file" id="lastOppDokument-0-VERGE-LEGITIMASJON"
accept=".pdf,.bmp,.gif,.jpeg,.jpg,.png,.tiff">
<label class="hb-knapp hb-knapp--standard hb-spinner-tekst" for="lastOppDokument-0-VERGE-LEGITIMASJON"> Velg fil </label><!---->
</span>
</td>
<td>
<span class="hb-felt knappesamling-gruppe hb-avstandIngen ng-star-inserted">
<input class="hb-lastOppFil-input hb-bare-skjermleser" type="file" id="lastOppDokument-1-VERGE-VERGEMAL"
accept=".pdf,.bmp,.gif,.jpeg,.jpg,.png,.tiff"><label class="hb-knapp hb-knapp--standard hb-spinner-tekst" for="lastOppDokument-1-VERGE-VERGEMAL"> Velg fil </label><!----></span>
<label class="hb-knapp hb-knapp--standard hb-spinner-tekst" for="lastOppDokument-1-VERGE-VERGEMAL"> Velg fil </label>
</span>
It shows the Stale Element Reference Exception when I try to check whether the alert is displayed or not.
I have used all the following techniques to handle the Alert dialogue box but it shows an Exception.
JS Executor
Robot class
WebDriver Wait
.click() method
Action class
All the Elements are present while I check manually. When I automate it, it shows an exception...
But I don't know where it occurs??!!...
Step Definition Code:
(In this code, I am using Action class)
public void alert_msg_display() throws Throwable {
WebElement x= driver.findElement(By.xpath("//button[#data-hover='LOGIN NOW']")); //Login button path
actionClick(driver, x);
WebElement y= driver.findElement(By.xpath("//md-dialog[#aria-label='Alert']/md-dialog-content/div")); // Alert message text path
String a= y.getText();
WebElement z= driver.findElement(By.xpath("//button[#ng-click='hide()']")); // Alert box close button path
actionClick(driver, z);
String a1 = "Please Enter Branch Id";
driver.findElement(By.xpath("//input[#ng-model='Branchid']")).sendKeys("HO");
actionClick(driver, x);
String b= y.getText();
actionClick(driver, z);
String b1 = "Please Enter Username (Email Id)";
driver.findElement(By.xpath("//input[#ng-model='Username']")).sendKeys("testmail");
actionClick(driver, x);
String c= y.getText();
actionClick(driver, z);
String c1 = "Please Enter Username (Email Id)";
driver.findElement(By.xpath("//input[#ng-model='Username']")).clear();
driver.findElement(By.xpath("//input[#ng-model='Username']")).sendKeys("xxxxx.rt#yyyyy.com");
actionClick(driver, x);
String d= y.getText();
actionClick(driver, z);
String d1 = "Please Enter Password";
driver.findElement(By.xpath("//input[#name='password']")).sendKeys("abcde");
actionClick(driver, x);
String e= y.getText();
actionClick(driver, z);
String e1 = "LOGIN FAILED, PLEASE CHECK YOUR USERNAME OR PASSWORD";
if (a.equals(a1) && b.equals(b1) && c.equals(c1) && d.equals(d1) && e.equals(e1))
test.log(LogStatus.PASS, "Test Case ID: LOG_006 to LOG_010 - Pass");
else
test.log(LogStatus.FAIL, "Test Case ID: LOG_006 to LOG_010 - Fail");
}
Runner File Code
public void actionClick(WebDriver driver, WebElement a) {
Actions action = new Actions(driver);
action.moveToElement(a).click().build().perform();
}
The movement you click on 'Login Now' (x) element the references to x & y will be refreshed. So you can't use the x & y that are pointing to the old references and that will lead to the Statle Element exception.
The solution would be to get the element each time you want to click on the x & y so that you will have the latest reference to the elements.
I want to print the submenu text of mainmenu of first list[Electronics] of
in selenium webdriver.
url : https://www.flipkart.com
But there is some issue to take the xpath of that sumMenu.
How can I take the xpath and all.
You can try with the following x-path to get all sub menus of main menu "Electronics"
//span[.='Electronics']/following-sibling::ul//li/a
Try the bellow code. Change value the String searchSubMenu = "Electronics"; if you want get text other sub menu, hope this helps.
driver.get("https://www.flipkart.com/");
//wait login popup and click
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#class='_2AkmmA _29YdH8']")));
driver.findElement(By.xpath("//*[#class='_2AkmmA _29YdH8']")).click();
String searchSubMenu = "Electronics";
int totalSubMenu = driver.findElements(By.xpath("//*[contains(#class,'Wbt_B2')]")).size();
System.out.println("Search for : " +searchSubMenu);
for(int i=1; i<=totalSubMenu; i++) {
String getTextSubMenu = driver.findElement(By.xpath("(//*[contains(#class,'Wbt_B2')])[" +i +"]")).getText();
System.out.println("Get Sub Menu Title : "+ getTextSubMenu);
if (getTextSubMenu.equals(searchSubMenu)) {
driver.findElement(By.xpath("(//*[contains(#class,'Wbt_B2')])[" +i +"]")).click();
Thread.sleep(1000);
String targetAllGetText = driver.findElement(By.xpath("(//*[contains(#class,'_3GtRpC')])[" +i +"]")).getText();
System.out.println(targetAllGetText);
break;
}
}
driver.quit();
It will help you : Please try
String SubMenu = driver.findElement(By.xpath("Xpath of element")).getText();
System.out.println(SubMenu);
if you want Size:
int Size =driver.findElement(By.xpath("Xpath of element"));
System.out.println(Size);
I am trying to drag and drop elements on a AngularJS demo site, but I am not able to achieve results.
For my practice I am using https://marceljuenemann.github.io/angular-drag-and-drop-lists/demo/#/nested , and I am trying to drag & drop 'Item 7 (From dropzone B)' to 'Item 3 (In container 1)'.
I have used .getText() method to ensure that my xpath is pointing to the correct element. But still it is not working. Below is my code:
public void SetUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "F:\\Drivers\\chromedriver.exe");
d = new ChromeDriver();
d.manage().window().maximize();
d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
d.get("https://marceljuenemann.github.io/angular-drag-and-drop-lists/demo/#/nested");
Thread.sleep(2000);
((JavascriptExecutor)d).executeScript("scroll(4,400)");
WebElement src = d.findElement(By.xpath("html/body/div[2]/div[2]/div[2]/div[1]/div[1]/div[2]/div/div/ul/li[1]/div"));
System.out.println("The src element is:" +src.getText());
WebElement destn = d.findElement(By.xpath("html/body/div[2]/div[2]/div[2]/div[1]/div[1]/div[1]/div/div/ul/li[1]/div/div[2]/ul/li/div"));
System.out.println("The src element is:" +destn.getText());
org.openqa.selenium.Point pt1 = src.getLocation();
org.openqa.selenium.Point pt2 = destn.getLocation();
int xCord1 = pt1.getX();
int yCord1 = pt1.getY();
int xCord2 = pt2.getX();
int yCord2 = pt2.getY();
System.out.println("The X & Y cordinate of the src is:" +xCord1+ " & " +yCord1);
System.out.println("The X & Y cordinate of the destn is:" +xCord2+ " & " +yCord2);
Actions ac = new Actions(d);
Action dragAndDrop = ac.clickAndHold(src).moveToElement(destn, 334, 661).release(destn).build();
dragAndDrop.perform();
}
I have the following HTML snippet:
<div class="alert alert-danger">
<button class="close" type="button" data-dismiss="alert" aria-hidden="true">×</button>
User name or password are incorrect
</div>
What will be the xpath to find the "User name or password are incorrect" text ?
Use Following xpath to get the message -
String message = driver.findElement(By.xpath(".//div[#class='alert alert-danger']")).getText();
or You can try using cssSelector as well
String message = driver.findElement(By.cssSelector("div.alert.alert-danger")).getText();
It returns your message with button text x . Use following code to get only message -
String message = driver.findElement(By.xpath(".//div[#class='alert alert-danger']")).getText();
String finalMessage = message.replace("x", "").trim();
Edited
Thread.sleep(2000);
String message = driver.findElement(By.xpath(".//div[#class='alert alert-danger']")).getText();
String finalMessage = message.replace("x", "").trim();
if(finalMessage.equals("User name or password are incorrect"))
{
System.out.println("Message is CORRECT.");
}
else
{
System.out.println("Message is INCORRECT.");
}
String WrongUsernameMessage = driver.findElement(By.xpath("//div[#class='alert alert-danger']")).getText();
String[] WUmessage= WrongUsernameMessage.split("\n");
//System.out.println(message[1]);
if (WUmessage[1].equals("User name or password are incorrect"))
{
System.out.println("Message is CORRECT.");
}else{
System.out.println("Message is INCORRECT.");
}
This is working.
You can use this xPath:
String message = driver.findElement(By.xpath(".//div[#class='login-content']/div/div")).getText();