Amazon click image based on search - selenium

I am very new to selenium and tried this and it didn't work. You can easily reproduce this by going to amazon site and search for hairclip and you will find this image in the search. Once this image is found, i want to go to next page but it is not happening.
System.setProperty("webdriver.chrome.driver", "C:\\software\\chromedriver_win32\\chromedriver.exe");
ChromeDriver Driver = new ChromeDriver();
Driver.get("http://www.amazon.com");
Driver.manage().window().maximize();
Driver.findElement(By.id("twotabsearchtextbox")).sendKeys("Hairclip");
Driver.findElementById("nav-search-submit-button").click();
By by = By.xpath("//img[contains(#src,'https://m.media-amazon.com/images/I/716AFuiNFoL._AC_UL320_.jpg')]");
WebDriverWait w = new WebDriverWait(Driver, 20);
WebElement element = w.until(ExpectedConditions.elementToBeClickable(by));
element.click();
Error as below
Expected condition failed: waiting for element to be clickable:
By.xpath:
//img[contains(#src,'https://m.media-amazon.com/images/I/716AFuiNFoL.AC_UL320.jpg')]
(tried for 20 second(s) with 500 milliseconds interval)
I appreciate your time for the reply and effort.

Looks like you wanna click on first image, in that case this xpath should work :
//img[#data-image-index='1']
try it like :
WebElement element = w.until(ExpectedConditions.elementToBeClickable(by.xpath("//img[#data-image-index='1']")));
element.click();

The XPath locator you are using //img[contains(#src,'https://m.media-amazon.com/images/I/716AFuiNFoL._AC_UL320_.jpg')] matches 4 elements on that page.
To click the element you want please use this locator:
(//img[contains(#src,'https://m.media-amazon.com/images/I/716AFuiNFoL._AC_UL320_.jpg')])[last()]
So the code line will be
By by = By.xpath("(//img[contains(#src,'https://m.media-amazon.com/images/I/716AFuiNFoL._AC_UL320_.jpg')])[last()]");

Related

Unable to find element with xpath for Add -ons text (Correct xpath)

driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.get("http://www.spicejet.com");
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//a[contains(text(),'Add-Ons')]")))
.build().perform();
Thread.sleep(3000);
driver.findElement(By.linkText("Hot Meals ")).click();
driver.close();
Unable to locate element with xpath //a[contains(text(),'Add-Ons')
Its it regarding frames?
your xpath can find the correct element when I verify it by manual. I suspect the page not load completely, please add some sleep after dirver.get() for debug purpose.
driver.get("http://www.spicejet.com");
Thread.sleep(10*1000) // sleep 10 seconds to wait page open completely.
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//a[contains(text(),'Add-Ons')]")))
.build().perform();
Sometimes exact element can't be found using By.linkText()
You just use cssSelector like below:
driver.findElement(By.cssSelector("#header-addons > ul > li:nth-child(5) > a")).click();
Thread.sleep(3000);
instead of the line driver.findElement(By.linkText("Hot Meals ")).click();
You can also use xpath instead like below:
driver.findElement(By.xpath("//*[#id=\"header-addons\"]/ul/li[5]/a")).click();
Thread.sleep(3000);

How to click a button created using <span> tag for Selenium webdriver?

I have span tag which looks like a button on html tag
<span class="middle">Next</span>
I tried using
xpath=driver.findElement(By.xpath(".//*[#id='modal-actions-panel']/div[2]/a/span/span/span")); // by considering fixed id as reference
Using absolute
xpath=driver.findElement(By.xpath("html/body/div[4]/div[2]/a/span/span/span")); // took this from firebug
and Using
driver.findElement(By.cssSelector("span[class='middle']"));
No success!! It is throwing below exception :
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//span[contains(., \"Next\")]"}
Command duration or timeout: 30.12 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
For all the ways I tried it is showing the same exception with change in selector details. Can someone please help me out in finding solution so that I can find Next button that is in span tag and click it.
Next button is in iFrame: Below is the part of html covering required span tag.
Next
I also tried with :
driver.switchTo().frame("iframe-applicationname_ModalDialog_0");
WebElement el = driver.findElement(By.cssSelector("span.middle"));
But throwing below error :
Caused by: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Kindly let me know if needed something that I'm missing..
I think this element is inside a frame or iframe, if it is then you need to switch that frame or iframe before finding element as below :-
driver.switchTo().frame("iframe-applicationname_ModalDialog_0");
WebElement el = driver.findElement(By.cssSelector("span.middle"));
el.click();
//Now after all your stuff done inside frame need to switch to default content
driver.switchTo().defaultContent();
Edited1 :- If you are getting exception as element is not currently visible need to implement WebDriverWait to wait until element visible as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
//Find frame or iframe and switch
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframe-applicationname_ModalDialog_0"));
//Now find the element
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//span[#class = 'middle' and contains(text(), 'Next')]")));
el.click();
//Once all your stuff done with this frame need to switch back to default
driver.switchTo().defaultContent();
Edited2 :- If unfortunately it's not getting visible try to click on it using JavascriptExecutor as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
//Find frame or iframe and switch
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframe-applicationname_ModalDialog_0"));
//Now find the element
WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//span[#class = 'middle' and contains(text(), 'Next')]")));
//Now click using JavascriptExecutor
((JavascriptExecutor)driver).executeScript("arguments[0].click()" el);
//Once all your stuff done with this frame need to switch back to default
driver.switchTo().defaultContent();
Try this....
driver.findElement(By.xpath("//span[contains(#class,'middle') and contains(text(), 'Next')]"))
I tried it and it worked for me:
WebElement actionBtn=driver2.findElement(
By.xpath("//span[contains(#class,'v-menubar-menuitem-caption')
and contains(text(), 'Actions')]")
);
actionBtn.click();
Try this
new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(#class,'middle') and contains(text(), 'Next')]"))).click();

PartialLink text for selecting value in selenium

Scenario: Search google with word 'Selenium' and click on the first link.
I have written this below code:
`WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
List<WebElement> alist = driver.findElements(By.partialLinkText("Selenium"));
System.out.println(alist.size());`
but it is giving me size as zero. Why?
Actually when you are going to find list of all links with partial text Selenium, all links are not present in the DOM due to fast execution. You should try using WebDriver to wait until all elements visible in the DOM as below :-
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
WebDriverWait wait = new WebDriverWait(driver, 10);
List<WebElement> alist = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.partialLinkText("Selenium")))
System.out.println(alist.size());
Output:-13
Hope it helps...:)
ur problem occurs because u don't wait after searching. just wait a little moment so that browser can search and show the result. use the below code :
driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
Thread.sleep(5000);
though this kind of wait is not recommended to use. Try to use wait by using until like:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("element css path")));
u can use by xpath or id or class here.

getting Element is not clickable at point (355, 160) exception

My script is failing because of the following exception.
org.openqa.selenium.WebDriverException: unknown error: Element is not
clickable at point (355, 160)
While loading the page if the element appears in the background, selenium tries to click and fails. I have used webdriverwait. Out of 10 times it fails around 3 times minimum.
How can I avoid/handle this without using Thread.sleep();
You should wait until invisibility of element using invisibilityOfElementLocated as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath('xpath of please wait loading...')));
After this you could perform click on target element
Hope it will work..:)
Use explicit wait
WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("myElement")));
// or (new WebDriverWait(driver, 10)).until(
// ExpectedConditions.visibilityOfElementLocated(By.id("myElement")));
myElement .click();

com.thoughtworks.selenium.SeleniumException: unknown error: Element is not clickable

Environment
Chrome : Version 39.0.2171.95
Chrome Driver : 2.13 (Latest)
Selenium WebDriver
I need to enter key in textbox and then click on enter button. Script is working fine in IE and FF. But when it comes to chrome I find an error
com.thoughtworks.selenium.SeleniumException: unknown error: Element is not clickable at point (221, 191). Other element would receive the click:
Some solutions which I saw is using
((JavascriptExecutor) driver).executeScript("window.scrollTo(0,\"+elementToClick.getLocation().y+\")");
but it dint work out for me.
Thanks in advance
Try waiting for the element to be clickable and then clicking on it.
You can use the below code for that:
//Wait for 20 seconds to detect that the element is clickable, and then clicking on it
try{
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//xpath of the Enter button")));
element.click();
}catch(Throwable e){
System.err.println("Error while waiting for the element to be clickable: "+e.getMessage());
}