I want to search Prime Video on Google Home Page and then I want to click on News Link on Google Search Page. I have used xpath to find this Link but while executing the code, I am getting NoSuchElementException. I have used below code, Please help me to know that why below code is not working ::
System.setProperty("webdriver.gecko.driver", "C:/Users/gecko/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
WebElement ele = driver.findElement(By.name("q"));
ele.sendKeys("prime video");
ele.submit();
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement news = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id='hdtb-msb-vis']//div[text()='News']")));
news.click();
driver.close();
Can you try with this one. I see you forgot to Click(); as well.
Unlocate element has a several causes. One of this cause is xpath is invalid or not found on that page. One way to check is using find.element then use your xpath if it does not found it will throw an exception. Here is an example.
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
/*wait page for 2 seconds -- simple way wait, but don't recommended for using real testing*/
Thread.sleep(2000);
driver.findElement(By.name("q")).Click;
driver.sendKeys("prime video");
driver.sendKeys(Keys.ENTER);
then try to verify if xpath is valid or invalid by using
try
{
driver.findElement(By.xpath("//*[#id='hdtb-msb-vis']//div[text()='News']")).Click;
}
catch(NoSuchElementException ex)
{
System.out.println("There is no element in this page or xpath is invalid : "+ex.Message);
}
catch(Exception ex)
{
System.out.println("Exception : "+ex.Message);
}
If xpath is invalid or not found, you may try Katalon Recorder or Chropath extension for chrome to let help to find xpath.
Katalon Record
https://chrome.google.com/webstore/detail/katalon-recorder/ljdobmomdgdljniojadhoplhkpialdid
Chropath
https://chrome.google.com/webstore/detail/chropath/ljngjbnaijcbncmcnjfhigebomdlkcjo?hl=en
It works for me with a slightly different xpath:
WebElement news = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\"hdtb-msb-vis\"]/div[2]/a")));
Related
I am doing automation using selenium webdriver (java) on a search engine BookMyCrop (http://www.bookmycrop.com). Here, I searched for a crop but, I am not able to click on desired search result. Please help me with it.
Code :
WebElement search = driver.findElement(By.xpath("//*[#id=\"search_keyword\"]"));
search.sendKeys("59825");
search.sendKeys(Keys.ENTER);
driver.findElement(By.partialLinkText("Cashew")).click();
------My 1st try-------------
//WebElement link = driver.findElement(By.xpath("\"//div[#id = 'Links']/a[3]\""));
//link.click();
------My 2nd try-------------
//List<WebElement> find = driver.findElements(By.xpath("/html/body/section[2]/div[2]/div/div/div/div[1]"));
//find.get(1).click();
}
} –
You can use the css selector based on class names: ".product-block.inner-product-block" and get the list of all the search results.
Then click on whatever index you want to click.
I am not using an IDE for this but it would look something like this:
driver.findElements(By.cssSelector(".product-block.inner-product-block")).get(0).click();
As said, you can try with css ".product-block.inner-product-block"
Then
get List of WebElements
do loop
inside loop, try get text of each element or innerText attribute
cross check if it is required one or not by simple if condition
If so, click on that web element and break loop
if this locator is not giving required info, try other locator. say $$("a h3") for veg names.
The below code worked for me. It is navigates to the correct link
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://www.bookmycrop.com");
WebElement search = driver.findElement(By.xpath("//*[#id=\"search_keyword\"]"));
search.sendKeys("59825");
search.sendKeys(Keys.ENTER);
driver.findElement(By.partialLinkText("Cashew")).click();
i'm need to click a button which may appear with a 50 percent chance, decided to use try/catch with findElementBy. Nevertheless try/catch doesn't work and I'm getting an exception. Maybe there is a more efficient way to handle that button?
driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
WebDriverWait wait = new WebDriverWait(driver,5);
try {
WebElement element = driver.findElement(By.xpath("buttonXpath"));
element.click();
}
catch (NoSuchElementException e){ }
Possibly you are seeing NoSuchElementException which can happen due to a lot of reasons. You can find a detailed discussion in NoSuchElementException, Selenium unable to locate element
Solution
The best approach would be to construct a Locator Strategy which uniquely identifies the desired element with in the HTML DOM following the discussions in:
How to inspect element for Selenium v3.6 as FireBug is not an option any more for FF 56?
FirePath not displaying HTML code after typing x-path
Now as per best practices while invoking click() always induce induce WebDriverWait with in a try-catch{} block for the elementToBeClickable() as follows:
try{
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("buttonXpath"))).click();
System.out.println("Element was clicked")
}
catch (TimeoutException e){
System.out.println("Element wasn't clicked")
}
This will work for you:
List<Webelement> element = driver.findElements(By.xpath("buttonXpath"));
if(element.size() > 0) {
element.get(0).click();
}
Use method to check if this element is on your screen:
if (!driver.findElementsByXPath("buttonXpath`enter code here`").isEmpty()) {
driver.findElementByXPath("buttonXpath`enter code here`").click();
}
I am trying to perform automation testing using Selenium with Java in Eclipse IDE.
I am finding xpath using the 'Inspect Element' option in chrome browser. However the same xpath is working fine in Firefox browser but NOT in chrome and IE. Can someone help me to solve this in chrome and IE? It throws me 'Element not visible' error in Chrome and IE.
Try this xpath and Add a wait before it e.g.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id='lenderName']")));
driver.findElement(By.xpath("//*[#id='lenderName']")).click();
You can try following options to find element :
driver.findElement(By.id("lenderName")).click();
driver.findElement(By.cssSelector(".lenderName")).click();
driver.findElement(By.xpath("//*[#id='lenderName']")).click();
If still not work then use Explicit wait with JavascriptExecutor :
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("lenderName")));
((JavascriptExecutor)BrowserUtilities.driver).executeScript("arguments[0].click()", element);
I am writing a script to locate "login" and click on it for an web based application but I am getting exception:
no such element: Unable to locate element
My code:
System.setProperty("webdriver.chrome.driver","D:\\Selenium\\drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.wayn.com"); //to find login
driver.findElement(By.xpath("//*[#id='TopMenu']/div[1]/div/div[2]/login-buttons/div/div[1]/div[1]"))
.sendKeys(Keys.ENTER);
Try this way.
driver.get("http://www2.wayn.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[#class='button big left red ng-isolate-scope'][#wayn-log-click='loginButtonsLogClick']")).click();
Explanation of xpath: Use class and wayn-log-click attribute along with <div> tag.
Suggestion :- Instead of using absolute xpath, use relative xpath.
try waiting a little for element to be visible:
WebDriverWait wait =new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("username")));
driver.findElement(By.name("username")).sendKeys("username");
driver.findElement(By.name("password")).sendKeys("password");
My problem mainly is that my code doesn't run, I tried for more than 2 hours. I have seen many posts also, but some are written in different computer languages (not in Java), so I am confused now.
Below is my code for just clicking a button. All I want to do is click a button and go to new page.
WebDriver driver = new HtmlUnitDriver();
driver.get("file:///C:/Users/Sanya/Desktop/New%20folder%20(2)/page%203%20alerts.htm");
WebElement element = driver.findElement(By.partialLinkText("Alert"));
element.click();
Try this it works fine for me:
WebElement menuHoverLink = driver.findElement(By.id("your_id"));
actions.moveToElement(menuHoverLink).perform();
You can try the below one...
Actions action = new Actions(driver);
action.click(driver.findElement(By.partialLinkText("Alert"))).build().perform();
It was worked for me :-)
You can use XPath for instance to locate the element on your page:
By locator = By.xpath("//li[#title='Alerts']/a");
WebElement element = driver.findElement(locator);
Here is more information about how XPath works.