not able to click on following link using selenium webdriver - selenium

I am not able to click on following link using selenium webdriver:
<center>
<a class="xyz" style="" href="/Folder">My Folders</a>
<span></span>
</center>
I am using the code:
abhiFX.findElement(By.partialLinkText("My Folders")).click();

I see these potential problems:
Are you sure that your HTML 'works' at all if you load the page in a browser and click on the link? What's the expected result of the click?
Is your driver abhiFX initialized properly? Does .click() on other elements work well?

Try to use xpath instead:
public void clickElement() {
try {
WebElement element = abhiFX.findElement(
By.xpath("//a[contains(text(),'My Folders')]"));
element.click();
} catch (InvalidSelectorException e) {
throw new AssertionError("[FAIL] Click Element: Xpath is invalid.");
} catch (NoSuchElementException e) {
throw new AssertionError(
"[FAIL] Click Element: Unable to locate element");
}
}

Related

Whenever i perform click operation its navigating to footer instead of moving that page, and test case getting failed.Please give us some solution

We have used JS click and normal click but it's moving to the footer.
public static void jsClick(WebDriver driver, WebElement element,String msg) {
try {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);
WaitUtils.staticWait(5000); //3000
LoggerHelper.log().info(msg);
} catch (Exception e) {
LoggerHelper.log().error("Not able to click", e.getCause());
}
}
You don't click on a button in Selenium by injecting as JavaScript into it. You should use the Selenium driver in order to click:
element = driver.findElement(By.CSS, 'button[id="ButtonID"]')
element.click()
In you code example you are not using the Selenium plugin.

Invalid selector exception for clicking an element using Selenium with Cucumber for W3Schools site

I am trying with the following Selenium Code using Cucumber for W3Schools site. When I click on "Try it yourself" button then it navigates to another page opening different window and the window control also goes to the new window opened. So,In the new window opened, if i click the run button, it throws an exception:
invalid Selector
code:
//This clicks on the Try it yourself button
#FindBy(how=How.XPATH,using="//*[#id=\"main\"]/div[4]/p/a")
private WebElement TryItYourself;
public void TryItYourSelfClick()
{
TryItYourself.click();
}
//Now,a new window opens up where I want to click on Run Button
#FindBy(how=How.LINK_TEXT,using="Run >>")
private WebElement RunButton;
public void RunClick()
{
RunButton.click();
}
Calling Run Method
#Then("^a new window should appear$")
public void a_new_window_should_appear() {
System.out.println("Run button before Clicking");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
obj1.RunClick();
System.out.println("Run after clicking");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Set <String> handle=driver.getWindowHandles();
String firstWinHandle=driver.getWindowHandle();
String WinHandle=handle.iterator().next();
if(WinHandle!=firstWinHandle)
{
driver.switchTo().window(WinHandle);
System.out.println("Working for new window opened");
}
}
why this invalid selector exception is coming?
HTML code:
<div class="w3-bar w3-light-grey" style="border-top:1px solid #f1f1f1;overflow:auto">
<a id="tryhome" href="https://www.w3schools.com" target="_blank" title="w3schools.com Home" class="w3-button w3-bar-item topnav-icons fa fa-home" style="font-size:28px;color:#999999;margin-top:-2px"></a>
<button class="w3-button w3-bar-item w3-green w3-hover-white w3-hover-text-green" onclick="submitTryit(1)">Run »</button>
<span class="w3-right w3-hide-medium w3-hide-small" style="padding:8px 8px 8px 8px;display:block"></span>
<span class="w3-right w3-hide-small" style="padding:8px 0;display:block;float:right;"><span id="framesize">Result Size: <span>433 x 439</span></span></span>
</div>
I'm not familiar with cucumber, but does it have an option to use cssSelector?
I looked at the button and found
body .trytopnav button
as a valid selector.
This error message...
invalid Selector Exception
...implies that the how=How.LINK_TEXT,using="Run >>" was not a valid selector.
The main issue is the element is not a LINK_TEXT but a <button> tag with text as Run ».
Solution
Change the Locator Strategy as follows:
#FindBy(how=How.XPATH,using="//button[contains(.,'Run »')]")
//or
#FindBy(how=How.XPATH,using="//button[contains(.,'Run')]")
private WebElement RunButton;
look here https://www.seleniumhq.org/exceptions/invalid_selector_exception.jsp we need to avoid using >> as possible, so try to use another locator by avoiding these

Unable to locate element when element is still present

Driver is unable to find element on the page even when its present
. Basically Im just trying that when a popup (ad popup which we see in many website) is present it should get clicked. Below is the code :
System.setProperty("webdriver.gecko.driver","C:\\SeleniumDriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.get("https://www.sportskeeda.com/wwe");
//WebElement popup=driver.findElement(By.className("bullbg"));
WebElement popup=driver.findElement(By.xpath("//div[contains(text(),'Close')]"));
if (popup.isDisplayed())
{
System.out.println("True");
}
else
{
System.out.println("False");
}
ERROR
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to locate element: //div[contains(text(),'Close')]
Please click here to check there is a popup which gets display
Try scrolling to the element first, something might be in the way
WebElement element = driver.findElement(By.id("id"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Use Instead:
Boolean popup=driver.findElement(By.xpath("//div[contains(text(),'Close')]")).isDisplayed();
if (popup == true)
{
System.out.println("True");
}
else
{
System.out.println("False");
}

selenium - submenu click not working

i am not able to click the submenu.. tried with different xpath/id .....
below is the html tags, Main Menu is Presentations(marked in red arrow) and submenus are under div.
can you please let me know how i can write xpath for this. i wanted to click hypothetical in the submenu.
here main menu tag is at the bottom of div(submenu).
also attached selenium code . please help me....
<div id="presentations" class="ToolbarSubMenu" align="left"parent="presentations_parent">
<a id="hypothetical" class="ToolbarMenu" href="">Hypothetical</a><br/>
</div>
<a id="presentations_parent" class="ToolbarMenu" href="">Presentations</a>
#Test
public void hypothetical()
{
WebElement ic = driver.findElement(By.id("presentations"));
Actions act = new Actions(driver);
// act.moveToElement(ic).click().build().perform();
//act.moveToElement(ic).doubleClick().build().perform();
act.moveToElement(ic).clickAndHold().release().build().perform();
//ic.click();
//driver.switchTo().window(myWindowHandle);
// driver.findElement(By.linkText("Hypothetical")).click();
// driver.findElement(By.xpath("//div[2][#id='presentations']/a[1]")).click();
//Actions act = new Actions(driver);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// WebElement hyp=driver.findElement(By.partialLinkText("Hypothetical"));
WebElement hyp=driver.findElement(By.id("//div[#id='presentations']/a[1]"));
//act.moveToElement(hyp).click().build().perform();
hyp.click();
Use below code:
//Click on main menu that will opens the sub Menu list
WebElement ic = driver.findElement(By.id("presentations"));
ic.click();
If your requirement is to click on each individual sub menu item,then use below code:
click for Hypothetical is:
ic.findElement(By.id("hypothetical")).click();
click for Profile is:
ic.findElement(By.id("profile")).click();
(Or)
You can also get all subMenu items at a time, like this:
//Get all the sub menu list.
List<WebElement> list = ic.findElements(By.tagName("a"));
for(int i=0;i < list.size; i++){
WebElement subMenuElement = list.get(i);
subMenuElement.click();
}
driver.findElement(By.id("presentations")).click();
WebElement hyp = driver.findElement(By.id("hypothetical"));
hyp.click();

How can i scroll the panel using Selenium Webdriver

I want to move down panel using selenium webdriver.
public void AddCode()
{
try
{
getChromeDriver().findElementByClassName("odd").click();
Thread.sleep(5000);
getChromeDriver().manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
/*I want to move down panel here.
Panel tag : <div id="icd9-tab-data">
Scrollbar tag : <div class="mCSB_dragger_bar" style="position: relative; line-height: 505px;"></div>*/
/* "add-ic9-diagnosis-code" link has been displayed in bottom of panel, so not able to click on this link without scrolling panel. */
getChromeDriver().findElement(By.id("add-icd9-diagnosis-code")).click();
Thread.sleep(3000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
I want to move down panel using selenium webdriver.
Panel tag :
Scrollbar tag :
Do you want something like:
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,10);");
//This code is tested to scroll the browser scrollbar
Or you can use this code to focus (move to) the element:
Actions action = new Actions(driver);
action.moveToElement(WebElement).perform();
If both are not as per your requirement, please elaborate more about your requirement.