PartialLink text for selecting value in selenium - 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.

Related

Amazon click image based on search

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()]");

while automating the slider, The slider moves only when i placed my mouse over it in selenium

http://automationpractice.com/index.php?id_category=5&controller=category#/
This is my code,whats wrong with it?
By locator = By.cssSelector(".ui-slider-handle.ui-state-default.ui-corner-all.ui-state-hover");
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
WebElement sliderRight = driver.findElement(locator);
action.dragAndDropBy(sliderRight,-40,0).build().perform();
action.moveToElement(sliderRight).click().build().perform();
Where's actions taking under consideration the driver.
Try to enter the following code,
Actions action=new Actions(WebDriver);
You have used a wrong cssSelector. I have modified your code and it should work now.
Here is the java code
System.setProperty("webdriver.chrome.driver", "src/chromedriver 3");
WebDriver driver = new ChromeDriver();
driver.get("http://automationpractice.com/index.php?id_category=5&controller=category#/");
Actions action = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, 30);
//modified the css selector
By locator = By.cssSelector(".ui-slider-handle.ui-state-default.ui-corner-all");
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
WebElement sliderRight = driver.findElement(locator);
action.dragAndDropBy(sliderRight,70,0).build().perform();
action.moveToElement(sliderRight).click().build().perform();

Nosuchelementfound exception in Selenium Webdriver

As a new learner for Selenium, I am trying to identify a basic element on the Google page for the Google Search button. However I am getting a NoSuchElementException even though the element is perfectly visible on the page.
Code Snippet :
String driverPath = "C:\\Users\\nchaudhary006\\IEDriverServer.exe";
System.setProperty("webdriver.ie.driver",driverPath);
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
InternetExplorerDriver driver = new InternetExplorerDriver(capabilities);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("https://google.com");
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.name("btnK"));
Solutions Tried :
Element until visible option
Scroll down so that page is in view
Identifying element by xpath, id , name, etc.
All possible wait combinations.
What can I be possibly doing wrong?
I am not sure to which element you exactly referred by name as btnK but on https://google.com there is a Search field by name as q
Using Selenium 3.4.0, IEDriverServer 3.4.0 & IE 10.x (& above) the below code will put "Neha Chaudhary" in the Search field.
A few words about the Solution:
Once you set ImplicitlyWait at the begining, it remains valid throughout your execution. So you don't have to mention again & again.
Instead of InternetExplorerDriver implementation you must use the WebDriver interface.
While you search for an element driver.findElement(By.name("btnK")); either store it as a WebElement for future use or perform some action by click() or sendKeys("Neha Chaudhary")
Here is the working code snippet:
String driverPath = "C:\\Utility\\BrowserDrivers\\IEDriverServer.exe";
System.setProperty("webdriver.ie.driver",driverPath);
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
WebDriver driver = new InternetExplorerDriver(capabilities);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://google.com");
driver.findElement(By.name("q")).sendKeys("Neha Chaudhary");
Let me know if this Answers your Question.

Popup scroll not working in selenium webdriver

I am running my script in mozilla firefox I want to scroll popup I
applied so many methods but doesn't work for me
I used keys.tab to reach that element but it was unable to enter text in that textfield using senkeys("xyz#gmail.com)
I used scroll method
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('youama-email').scrollIntoView(true);");
some exception occur
3.I used Moveto element method but got exception
WebElement element = driver.findElement(By.id("youama-email"));
Actions
actions = new Actions(driver);
actions.moveToElement(element);
actions.click();
actions.perform();
// Initialize Javascript executor
JavascriptExecutor js = (JavascriptExecutor) driver;
// Scroll inside web element vertically (e.g. 100 pixel)
js.executeScript("arguments[0].scrollTop =
arguments[1];",driver.findElement(By.id("<div-id>")), 100);
Please help me to scroll and enter into the email as well as other
fields that will appear after scroll [![enter image description
here][1]][1]
[1]: http://i.stack.imgur.com/D0hqI.png
Try this code. I think what you didn't do was to wait for the element to be visible which i did. See the below code. It is running correctly.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://wyomingvapor.com/");
driver.findElement(By.xpath(".//*[#id='header']/div/div[2]/div/a[1]")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='y-to-register']/input")));
driver.findElement(By.xpath(".//*[#id='y-to-register']/input")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='youama-firstname']")));
driver.findElement(By.xpath(".//*[#id='youama-firstname']")).sendKeys("xyz#gmail.com");
Thread.sleep(2000L);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_X);
robot.keyRelease(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_Y);
robot.keyRelease(KeyEvent.VK_Y);
robot.keyPress(KeyEvent.VK_Z);
robot.keyRelease(KeyEvent.VK_Z);
If you still stuck then do reply to me, Jyotsana.
Happy Learning :-)

Actions and htmlunitdriver - speed issue

My web application has menus that open on MouseOver. I'm writing tests using htmlunitdriver.
Test code to trigger menu is
Actions builder = new Actions(driver);
WebElement menu = driver.findElement(By.xpath("//a[starts-with(#href,'/index.html')]"));
Thread.sleep(2000);
builder.moveToElement(menu).build().perform();
Thread.sleep(2000);
driver.findElement(By.xpath("//a[starts-with(#href,'/submenuitem')]")).click();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
When I run a single test, it passes just fine. But when I try to run all my 80 tests at once, I get
unable to locate node using //a[starts-with(#href,'/submenuitem'
I guess the submenu is not yet open, htmlunitdriver has too much speed. Somethimes a "You may only interact with elements that are visible is occured on single runs too. Can someone help me fix this issue? Using FirefoxDriver or so is not an option for me.
Using a manual Thread.sleep(time) to wait for selenium actions is a dirty solution and should not be used at all.
Instead you could run a check is the element is visible before interacting with it.
public void waitUntilVisible(WebDriver driver, WebElement element){
WebDriverWait waiting = new WebDriverWait(driver, 10);
waiting.until(ExpectedConditions.visibilityOf(element));
}
public void waitUntilClickable(WebDriver driver, By locator){
WebDriverWait waiting = new WebDriverWait(driver, 10);
waiting.until(ExpectedConditions.elementToBeClickable(locator));
}
Actions builder = new Actions(driver);
WebElement menu = driver.findElement(By.xpath("//a[starts-with(#href,'/index.html')]"));
waitUntilVisible(driver, menu);
builder.moveToElement(menu).build().perform();
WebElement menuItem = driver.findElement(By.xpath("//a[starts-with(#href,'/submenuitem')]"));
waitUntilClickable(driver, By.xpath("//a[starts-with(#href,'/submenuitem')]"));
menuItem.click();
You are using the implicit wait after finding the submenu item. I think there is no use for implicit wait there. The most advisable place to use the implicit wait is to declare after initializing the Driver instance.
One More solution you can use Explicit Wait to wait for an element in the Page.
Refer this post for more info about the Selenium waits.