I am new to Selenium webdriver, and trying to test a webpage which has some dynamic links (dynamic ads). Example : ads on https://mail.rediff.com/cgi-bin/login.cgi
I tried with xpath ,classname and id but none of these is working.It is happening because every time new content is displaying on the page so it is not able to find the element and throwing Exception in thread "main":
org.openqa.selenium.NoSuchElementException: Unable to locate element: #map.
my code is :
package Selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Image_Link {
static WebDriver driver ;
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
//System.setProperty("webdriver.chrome.driver", "E://chromedriver_win32//chromedriver.exe");
//driver = new ChromeDriver();
System.setProperty("webdriver.gecko.driver", "E://geckodriver-v0.21.0-win64//geckodriver.exe");
driver = new FirefoxDriver();
Actions actions = new Actions(driver);
driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
//WebElement Dynamic_ads=driver.findElement(By.className("rhs-area floatR"));
WebElement Dynamic_ads=driver.findElement(By.id("map"));
actions.moveToElement(Dynamic_ads).perform();
WebElement ad_Link = driver.findElement(By.cssSelector("#map > area:nth-child(2)"));
actions.moveToElement(ad_Link);
actions.click();
actions.perform();
//driver.navigate().to("www.google.com");
//String value = driver.findElement(By.id("hplogo")).getAttribute("title");
}
}
The elements for the dynamic ads cannot be found because it isn't fully loaded yet on load page. I recommend adding an explicit wait time for the specific add you're looking for. You can check out this link here for more information.
Anyhow, here's your solution:
1.)Implement WebDriverWait: WebDriverWait wait = new WebDriverWait(driver, 20);
2.) Change your Dynamic_ads data to wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("map"))); then do Dynamic_ads.click(); afterwards.
OR
2.) Change your Dynamic_ads data to wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//map[#id='map']/*"))); then do Dynamic_ads.click(); afterwards. NOTE: This will pick the first child node.
Related
I have tried various options, but have been unable to simulate a mouse click to drag an element from one position to another in a browser using Selenium. When the test runs, I see the element get selected, but it does not move to the specified drop point. Any suggestions or insights are greatly appreciated!
Here's how I defined the function in my latest attempt (variations on this theme also tried and failed):
private void dragAndDrop(WebElement dragPoint, WebElement dropPoint, WebDriver driver) {
Actions builder = new Actions(driver);
builder.clickAndHold(dragPoint).perform();
builder.pause(Duration.ofSeconds(1));
builder.moveByOffset(10,0).perform();
builder.moveToElement(dropPoint).perform();
builder.moveByOffset(10,0).perform();
builder.pause(Duration.ofSeconds(1));
builder.release();
builder.build();
builder.perform();
}
Also tried the following (same result):
private void dragAndDrop(WebElement dragPoint, WebElement dropPoint, WebDriver driver) {
Actions builder = new Actions(driver);
Action dragAndDrop = builder.dragAndDrop(dragPoint, dropPoint).build();
dragAndDrop.perform();
}
In the test, the 2 elements are identified uniquely using xpath and the function is called:
WebElement dragPoint = driver.findElement(By.xpath(".../div[3]/...(etc.)/div[#class='rst__moveHandle']"));
WebElement dropPoint = driver.findElement(By.xpath(".../div[5]/...(etc.)/div[#class='rst__moveHandle']"));
dragAndDrop(dragPoint, dropPoint, driver);
Relevant libraries:
react-beautiful-dnd: https://github.com/atlassian/react-beautiful-dnd
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
Unable to identify the webelements on XHTML page
In My project if I click on a link a PDF opens up in a new tab and I am trying to download the PDF by clicking on Download click.The Web Element is not getting identified as the download icon is on the XHTML page. I have tried replicating this using India Post website. Still unable to identify the element. Below is the code. Does anyone have a solution?
package com.demo.learn;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class handle {
public static void main (String [] args){
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "D://CAHCO Automation//OpteamixAutomation-master(1)//OpteamixAutomation-master//Automation_framework//OPTEAMIX//Drivers//chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://www.indiapost.gov.in/VAS/Pages/Form.aspx");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[#id='ctl00_SPWebPartManager1_g_29491a13_ee77_4e11_a5e4_e031cc13d245']/div[1]/table[1]/tbody/tr[2]/td[1]/a/img")).click();
Set<String> handles=driver.getWindowHandles();
String firsthandle = driver.getWindowHandle();
handles.remove(firsthandle);
String winhandle = handles.iterator().next();
if (winhandle!=firsthandle){
String secondWinHandle = winhandle;
driver.switchTo().window(secondWinHandle);
System.out.println("Switched");
boolean display = driver.findElement(By.xpath(".//*
[#id='download']")).isDisplayed();
if(display==true)
{
System.out.println("Displayed");
}
else
{
System.out.println("Not Displayed");
}
}
}
}
I guess what you are trying to do is clicking on PDF viewer in Chrome. The Chrome PDF viewer is a Chrome plugin so you cannot access it.
I recommend a workaround by adding an attribute to the link to open PDF. The attribute download will tell web browser to download it instead of open it in PDF Viewer. See my code below.
String script = "document.querySelector('#ctl00_SPWebPartManager1_g_29491a13_ee77_4e11_a5e4_e031cc13d245>div>table>tbody>tr>td>a>img').setAttribute('download','name-of-the-download-file-recommend-guid-or-timestamp.pdf');";
((JavascriptExecutor)driver).executeScript(script);
driver.findElement(By.xpath(".//*[#id='ctl00_SPWebPartManager1_g_29491a13_ee77_4e11_a5e4_e031cc13d245']/div[1]/table[1]/tbody/tr[2]/td[1]/a/img")).click();
Please note that the Css Selector in my example #ctl00_SPWebPartManager1_g_29491a13_ee77_4e11_a5e4_e031cc13d245>div>table>tbody>tr>td>a>img is just an example which was converted from your XPath. It might find an incorrect element.
I am trying to write a code to find cheapest tickets between two locations .I am stuck in the beginning of the code because selenium does not recognise simple input text box . here is the code .
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class orbitzsimpleTest {
public static void main(String args[]) throws InterruptedException {
WebDriver dr = new FirefoxDriver();
dr.navigate().to("https://www.orbitz.com/");
Thread.sleep(5000);
System.out.println("after threat sleep");
dr.findElement(By.xpath(".//*[#id='flight-origin']")).sendKeys("SFO");
}
}
I also tried to write the XPath using contains like
List<WebElement> s = dr.findElements(By.xpath(".//*[contains(#id,'flight_*')]"));
No luck. It looks so simple but i'm struggling to go past this.
You can just get this element with dr.findElement(By.id('flight-origin')) or (By.cssSelector('#flight-origin')).
And it's better to use 'smart' waits (wait for element to be displayed/clickable/visible) instead of sleep. Because it can be that 5 secs is not enough and you try to send key to the input which isn't loaded yet.
I think i found the reason why it was not recognising that element. It's because the flight tab had to be selected before it finds this text box .I was able to fix it .
Thank you so much for your input .
I'm new in Selenium webdriver and learning the Dynamics table as the moment im stuck at point. i want to click particular company name in dynamics table i have written sample scripts for it please let me whats wrong with it.
im using icicidirect website.
Selecting the Market link from main menu bar
Now at the bottom of the page their is one link "Daily Share Prices" link (its below the "Top Losers" section will get it by using ctrl+f)
At Daily Share Prices in first column (Security Name) i.e.ABB link element is their
and i want to click that element
public static void main(String[] args) throws Exception {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.icicidirect.com");
Thread.sleep(1000);
driver.findElement(By.xpath("//a[contains(text(),'Markets')]")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//a[contains(text(),'Daily Share Prices')]")).click();
Thread.sleep(3000);
TablePageObject tablePageObject = PageFactory.initElements(driver, TablePageObject.class);
tablePageObject.clickLink("ABB");
}
}
public class TablePageObject {
private WebDriver driver;
#
FindBy(css = "table tr")
private List < WebElement > allTableRows; // find all the rows of the table
public TablePageObject(WebDriver driver) {
this.driver = driver;
}
public void clickLink(String SecurityName) {
for (WebElement row: allTableRows) {
List < WebElement > links = row.findElements(By.linkText("ABB"));
// the first link by row is the company name, the second is link to be clicked
if (links.get(0).getText().contains(SecurityName)) {
links.get(0).click();
}
}
}
}
Several suggestions:
You may use the following link in order to receive the required table directly
driver.get("http://content.icicidirect.com/newsiteContent/Market/MarketStats.asp?stats=DailySharePrices");
You may wait for the table loading
Then you will found the element and click it (like in your code).
This is code that works for me
WebDriver driver = new FirefoxDriver();
try{
driver.get("http://content.icicidirect.com/newsiteContent/Market/MarketStats.asp?stats=DailySharePrices");
(new WebDriverWait(driver, 10/*sec*/)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("ABB")));
List<WebElement> dailyList = driver.findElements(By.linkText("ABB"));
if (dailyList.size()!=0) {
dailyList.get(0).click();
}
}
catch (Exception e) {
e.printStackTrace();
}
finally{
driver.close();
}
If you need find some element that not located on the first page you may extend this solution to click on the Next>> link and back to this loop improving it by removing hard coded "ABB" element.
I want to use JavaScript for my script.
I have created an object of JavaScriptExecutor, but executeScript() method is not present. It shows error when I use executeScript() method.
This is the code I have used:
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.JavascriptExecutor;
public class GetDomain_JS {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html");
driver.manage().window().maximize();
System.out.println(driver.getCurrentUrl());
JavaScriptExecutor js=(JavaScriptExecutor) driver;
String domain_name=(String) js.executeScript("return document.domain");
System.out.println(doamin_name);
}
}
It works for me; you had a mistake on JavaScriptExecutor with upper case S. Instead, you should have javascriptExecutor with lower case s.
Try this code:
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public class GetDomain_JS {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html");
driver.manage().window().maximize();
System.out.println(driver.getCurrentUrl());
JavascriptExecutor js=(JavascriptExecutor) driver;
String domain_name=(String) js.executeScript("return document.domain");
System.out.println(domain_name);
}
}
This works for me!! Please thumps up if it does for you!
Please make sure you have imported the correct package.
Expected package for working with Java Script:
import org.openqa.selenium.JavascriptExecutor;
Try this package. This should solve your error.
Explanation:
Add latest jar (I'm using 3.0 beta selenium jar). Import Javascript library package. Take web driver object by casting to JavascriptExecutor and run whatever java script you want to run.
Code:
import com.thoughtworks.selenium.webdriven.JavascriptLibrary;
Object ob = ((JavascriptExecutor) webDriver()).executeScript("return document.domain").toString();
System.out.println(ob);
You can return an Object from executeScript. Later you can get the text out of it.
Object domain_name = js.executeScript("return document.domain");
System.out.println(domain_name.toString());
In this way, you can return values of any type and not just string.