selenium and clicking an "a" with href=javascript - selenium

I'm trying to click a link and am having difficulties. The relevant HTML code is:
<div id="adHocAddDocDiv" style="display: block;">
<a href="javascript:hideDiv();" style="color:#000">
Close window
</a>
<table border="0">
<tbody></tbody>
</table>
</div>
For code, I have:
driver.findElement(By.xpath("//*[#id='adHocAddDocDiv']/a")).click();
This does find the correct element, however it doesn't seem to execute the JavaScript to close the window that happens if I manually click the link. Any ideas?
UPDATE: Here is the code that finally worked:
WebElement element = driver.findElement(By.xpath("//[#id='adHocAddDocDiv']/a"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

I frequently come across elements that WebDriver doesn't seem to be able to click. In these cases I use the following pattern:
var js = (IJavaScriptExecutor)driver;
js.ExecuteScript("$j(\"div[id='adHocAddDocDiv']\").click();");
This is the C# version. I'm sure the Java form is quite similar.

Try more explicit:
driver.findElement(By.linkText("Close window")).click();

My guess is that there are more <a>'s immediately following that div, and it's not unique enough. Try this:
driver.findElement(By.cssSelector("div#adHocAddDocDiv > a[href*='hideDiv()']")).click()

Related

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="card-id-oidc-i"]/a"}

I'm having this strange error (sometimes works sometimes does not):
no such element: Unable to locate element:
{"method":"xpath","selector":"//*[#id="card-id-oidc-i"]/a"}
My button has this:
<a href="/auth/realms/Global-Realm/broker/id-oidc-i/login?client_id=web&tab_id=Doz54nelUC0&session_code=gwAePmGfpQ2hBLommJO7Rswc1gNkB90Ctc4">
<div style="width:100%;height: 40px;">
<span class="arrow arrow-bar is-right"></span>
</div>
<div class="image" style="background-repeat: no-repeat;margin:auto; width:115px;height:120px"></div>
<div style="margin-top: 10px;min-width:170px">
<h4 style="text-align:center;"><b>log in</b></h4>
</div>
</a>
It's XPATH is:
//*[#id="card-id-oidc-i"]/a
I did this:
driver.findElement(By.xpath("//*[#id=\"card-id-oidc-i\"]/a")).click();
It is strange because sometimes works just fine but sometimes it fails.
Why?
You probably missing a delay.
Try using this:
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='card-id-oidc-i']/a"))).click();
BTW your locator is based on some parent element with id = card-id-oidc-i while you shared here only the child a element HTML.
Also, no need to put \ before " inside a String. You can simply use ' instead as I do here.
NoSuchElementException error may occur when :
HTML element may not be present in a DOM yet. So you have to implement WebDriverWait to wait until element is present and visible in a web page.
HTML element may not be inside frame or iframe.
Maybe in your case it is not in the DOM yet, try to wait until it is visible and on clickable position.

how to get element inside custom tag in chrome web driver

This is my sample html code.
<div class="content">
<M class="mclass">
<section id="sideA">
<div id="mainContent">
<div class="requestClass">
<span>Check</span>
<input type="text" id="box">
</div>
</div>
<section>
<section id="sideB">
...
<section>
</M>
</div>
I want to set some value to my text field ("box"). So I tired to set like below code
driver.findElement(By.xpath("...")).sendKeys("SetValue");
My Xpath id is correct, it's exist in the page but am getting this error
no such element: Unable to locate element: {"method":"xpath","selector":"id("..."}
Why I am getting this error because of my custom tag,if yes how to get element inside custom tag?
As per the HTML you have provided to fill in some value to the text field represented by <input type="text" id="box"> you can use either of the following line of code:
cssSelector :
driver.findElement(By.cssSelector("section#sideA input#box")).sendKeys("SetValue");
xpath :
driver.findElement(By.xpath("//section[#id='sideA']//input[#id='box']")).sendKeys("SetValue");
If you still want to use XPath. This worked for me-
driver.FindElement(By.XPath(#"//*[#id='box']")).SendKeys("AB‌​");
I don't think the custom tag causes any problem as the CssSelector also works-
driver.FindElement(By.CssSelector(#"m[class='mclass'] input")).SendKeys("AB");
You can use ID or xpath to locate it, My suggestion you have to use ID. Also use Explicit wait till element to be visible.
Using ID, your code is like this:
WebElement elem= driver.findElement(By.id("box"));
WebDriverWait wait=new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(elem));
elem.sendKeys("test");
You can also use JavascriptExecutor
WebElement elem= driver.findElement(By.id("box"));
WebDriverWait wait=new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(elem));
JavascriptExecutor myExecutor = ((JavascriptExecutor) driver);
myExecutor.executeScript("arguments[0].value='test';", elem);

Selenium javascript link

Could anyone please help me to hit the javascript link through selenium code.
This is the inspect code and link name is Applications.
Currently I am using below all the list of code, but still it's not hitting the applications link.
driver.findElement(By.id("I6")).click();
driver.findElement(By.xpath("//img[# src='/ibm/console/images/arrow_collapsed.gif']")).click();
driver.findElement(By.xpath("//span[contains(text(),'Applications']")).click();
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.xpath("//*[contains(text(),Applications')]"));
js.executeScript("arguments[0].click;", element);
WebElement element1 = driver.findElement(By.xpath("//span[contains(text(),'Applications']"));
element1.click();
driver.findElement(By.xpath("//a[#href='javascript:expandCollapse('6');]")).click();
driver.findElement(By.xpath("//div[#id='I6']/..//a[contains(text(),'Applications')]")).click();
Try to click WebElement using javascript this way.
WebElement element = driver.findElement(By.xpath("//span[contains(text(),'Applications']"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
I have try with your this html code:
<img src="/ibm/console/images/arrow_expanded.gif" title="Collapse" alt="Collapse" id="I6" border="0" align="absmiddle"> <img src="/ibm/console/images/arrow_collapsed.gif" title="Expand" alt="Expand" id="I6" border="0" align="absmiddle"> <a style="color:#000000;text-decoration:none;" href="javascript:expandCollapse('6');" title="Applications"><img src="/ibm/console/images/arrow_collapsed.gif" title="Expand" alt="Expand" id="I6" border="0" align="absmiddle"><span dir="ltr">Applications</span></a>
Refer Image for more details:
Correct me if I'm wrong, there is typo in all of your xpath like :
driver.findElement(By.xpath("//span[contains(text(),'Applications']")).click(); \\missed `)` in contains method
If see carefully somewhere ' missing and somewhere () , Ok leave it, try the following xpath and let us know still face same issue
driver.findElement(By.xpath("//a[#title='Applications']")).click();
or
driver.findElement(By.xpath("//a[contains(.,'Applications')]")).click();

Xpath not working as expected while running the test

I am trying to automate the browser, while I try to locate the element via xpath in browser in static mode it is able to highlight the element where as when I run the script it comes back with an error that it is unable to find the element.
xpath I have written:
driver.findElement(By.xpath("//input[#value='soa_b_pbtv_l0_trnkni']/following-sibling::td[1]/child::select[#name='jobaction']")));
Here is the HTML:
<form name="f2" onsubmit="return verify();" action="/ATS/cgi-bin/barcap_jobaction.pl" method="post">
<>
<input name="jobname" type="hidden" value="soa_b_pbtv_l0_trnkni"/>
<input name="jobinstance" type="hidden" value="D03"/>
<input name="jobproceed" type="hidden" value="web"/>
<td style="background-color: #ffffff;">
<select name="jobaction">
if you're trying to select the select, jobaction then try this:
use css selector for the select select[name='jobaction']
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("form[name='f2']")));
List<WebElement> eleList = driver.findElements(By.cssSelector("form[name='f2']")));
for(WebElement element: eleList) {
if(element.findElement(By.cssSelector("input[name='jobname']")).getText().equalsIgnoringCase("expectedValue")) {
WebElement element = element.findElement(By.cssSelector("select[name='jobaction']"));
}
}
The INPUT is hidden so it won't be found using typical Selenium means. Selenium was designed to only interact with elements that a user can see and interact with. You are able to locate it in the browser because you are using JS or JQuery and they are not designed to ignore hidden elements. One way to get around this is to use JavascriptExecutor... it basically allows you to run JS in Selenium and find hidden elements. Since it sounds like you already have successful locators, I would suggest you look up some tutorials on JSE and you should be set.
If you run into a new issue while using JSE, come back and post a new question and we can try to help you.

Selenium clicking a sub menu

I am Really stuck for the past two days here. I am trying to click sub menu and when I try to click sub menu I get an errors as like the following
Element not found for the sub menu.
I have tried below code
WebElement element = driver.findElement(By.id("x-menu-el-P46915788081933044__folderbrowser_PJL"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
HTML Code
<li id="x-menu-el-P46915788081933044__folderbrowser_PJL" class="x-menu-list-item">
<a id="P46915788081933044__folderbrowser_PJL" class="x-menu-item" href="javascript:var abc = doNothing()" unselectable="on" hidefocus="true">
<img id="ext-gen926" class="x-menu-item-icon " src="netmarkets/images/import.gif">
<span id="ext-gen927" class="x-menu-item-text">Upload Documents from Compressed File</span>
Instead of using the ID you should probably use the class name.
WebElement element = driver.findElement(By.ClassName("x-menu-list-item"));
or you could try using the css selector
WebElement element = driver.findElement(By.cssSelector("li[class='x-menu-list-item']"));
Since the above return multiple items you could just use to return the exact element that you need:
WebElement element = driver.findElement(By.linkText("Upload Documents from Compressed File"));
1st click on the menu and then try the following statement -
driver.findElement(By.xpath("//li[#class='x-menu-list-item']//span[contains(text(),'Upload Documents from Compressed')])).click();
or directly try this -
driver.findElement(By.xpath("//span[contains(text(),'Upload Documents from Compressed')])).click();
I guess mostly the error is due to the span name spaces in between words, if the above dont work, pls attach a screenshot or give some details of the html code, so we can try more options, all d best.