I am using selenium webdriver. I am unable to access link menu options.eg:I want to access options "Casual shoes" option under "Men" menu link from flipkart site. i tried using below code
WebElement a= driver.findElement(By.xpath("//a[title='Men']"));
a.click();
but unable to click on menu link "Men"
Your XPath is wrong you forget to add # in front of attribute. You are using //a[title='Men'] but you should use //a[#title='Men']
Below code is working for me:-
driver.get("http://www.flipkart.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//a[#title='Men']")).click();
driver.findElement(By.xpath("//span[contains(.,'Casual Shoes')]")).click();
OR
In chrome below code is working fine for me:-
WebElement we =driver.findElement(By.xpath("//a[#title='Men']"));
we.click();
WebElement Causual =driver.findElement(By.xpath("//span[contains(.,'Casual Shoes')]"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", Causual);
Hope it will help you :)
Related
There is a button on a website I am testing and Selenium can't seem to click it. I have tried many different ways but still nothing. Attached is the HTML for the button as well as the XPath.
Has anyone else experienced this or knows how to get around this?
XPath:
/html/body/div[1]/div/div/form/div[21]/div[3]/button
[HTML]
Could be a synchronization issue. kindly try below solution if your element is not within iframe or else you need to switch control to iframe first before dealing with the web element.
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"))).click();
Or You can also try javascript solution:
WebElement element= driver.findElement(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Try below code -
Actions action = new Actions(driver);
WebElement My_btn = webdriver.findElement(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"));
action.moveToElement(My_btn).click(My_btn).build().perform();
Let me know the outcome.
I want to scroll down my web page. What Java command should I use in Selenium?
Scrolling down a web page is not a valid usecase which can be validated perhaps you want to scroll down to bring a WebElement within the Viewport to interact with it. To achieve that you can use the executeScript() method as follows :
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", element);
Use the below code and try,
JavascriptExecutor js = (JavascriptExecutor) driver;
// Launch the application
driver.get("http://demo.guru99.com/test/guru99home/");
//This will scroll the web page till end.
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
Please refer the code below :
1) Using Action class****
WebDriver driver = new ChromeDriver();
//Creating an object 'action'
Actions action = new Actions(driver);
//open SoftwareTestingMaterial.com
driver.get(Site URL); // Give site URL as name of the web page
//sleep for 3secs to load the page
Thread.sleep(3000);
//SCROLL DOWN
action.sendKeys(Keys.PAGE_DOWN).build().perform();
Thread.sleep(3000);
//SCROLL UP
action.sendKeys(Keys.PAGE_UP).build().perform();
Before tagging this as duplicate. Please read the Question. i have seen many answers for this kind of question. But none of them really worked.
System.setProperty("webdriver.gecko.driver", "src/test/driver/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
driver.get("https://www.facebook.com");
this my code. When I run this instead of opening a new tab its just opening in Current tab.How can i open second link in new tab?
You can use javascript for this in ur selenium code :-
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.open('https://www.google.com','_blank');");
Help me to find out the xpath for button "click for JS Alert"
Here is the link https://the-internet.herokuapp.com/javascript_alerts
I am using this xpath-
.//*[#id='content']/div/ul/li[1]/button
and it showing no such element.
This is the code used:
public void test() {
driver.get("the-internet.herokuapp.com/javascript_alerts");
WebElement element=driver.findElement(By.id("//button[text()='Click for JS Alert']"));
element.click();
}
You use code in a wrong way:
WebElement element=driver.findElement(By.id("//button[text()='Click for JS Alert']"));
By.id can be applied to search element with specified id attribute only while you use XPath instead.
You need to use below:
WebElement element=driver.findElement(By.xpath("//button[text()='Click for JS Alert']"));
element.click();
//*[#id='content']/div/ul/li[1]/button works for me. What browser are you using? How are you validating?
Use this xpath: (//div[#class='example']/ul/li/button)[1]
Your code will be somewhat like this:
driver.get(""the-internet.herokuapp.com/javascript_alerts");
WebElement element=driver.findElement(By.xpath("(//div[#class='example']/ul/li/button)[1]"));
element.click();
Try this below code.
driver.get("https://the-internet.herokuapp.com/javascript_alerts");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath("//button[contains(text(), 'Click for JS Alert')]")).click();
Thread.sleep(2500);
Alert alert = driver.switchTo().alert();
System.out.println(driver.switchTo().alert().getText());
alert.accept();
I am using XPath, HtmlUnitDriver & FirefoxDriver.
Here is the my xpath to click on the signin button. When I use XPath
//div[#id='mainPane']/form/table/tbody/tr[10]/td/a[2]
to click on sign in button then it is working fine with FirefoxDriver but not working in HtmlUnitDriver.I have also enabled & disabled the javascript in HtmlUnitDriver.But not working.
I am getting Error:- When I click on this sign in button using HtmlUnitDriver,then it is not redirecting to next page,but in FirefoxDriver,it is redirecting to next page.Here is my code:-
HtmlUnitDriver driver = new HtmlUnitDriver(false);
WebElement webElement = webDriver.findElement(By.xpath("//div[#id='mainPane']/form/table/tbody/tr[10]/td/a[2]"));
webElement.click();
Please guide me.
As #David Grant suggests your problem might be related to the Javascript.
Enable your javascript in the HtmlUnitDrive -
HtmlUnitDriver driver = new HtmlUnitDriver(true);