Firefox driver unable to detect webelement - java

I have been trying to get the anchor link via WebDriver but somehow, things aren't working as desired and I am not getting the element.
Below is the HTML Structure:
<div id="1">
<table width="100%">
<tr>
<td>.....</td>
<td>
<ul class="bullet">
<li>....</li>
<li>....</li>
<li>
myText
</li> // myText is the text I am searching for
</ul>
</td>
</tr>
</table>
<div>....</div>
</div>
The <li> elements contains anchor tags with links only. No id or any other attribute they contain. The only difference is the text displayed by them and hence, I am passing myText to detect exactly what I need.
And for this, the java code I have been trying is:
driver.get("url");
Thread.sleep() //waiting for elements to get loaded. Exceptional Handling not done.
WebElement divOne = driver.findElement(By.id("1"));
WebElement ul = divOne.findElement(By.className("bullet"));
WebElement anchor = null;
try{
anchor = ul.findElement(By.partialLinkText("myText"));
}catch(NoSuchElementException ex){
LOGGER.warn("Not able to locate tag:" + linkText + "\n");
}
String myLink = anchor.getAttribute("href"); // null pointer exception
I don't understand why is this happening. What is the correct way to do this? Should I use some other method?

driver.findElement(By.xpath("//a[contains(text(),'myTest')]"));
that searches for myTest text. I haven't tried the code, I hope it helps you

you can use any of the below. Should work for you
List<WebElement> anchor = driver.findElements(By.partialLinkText("myText"));
or
driver.findElements(By.linkText("myText"))
or
driver.findElements(By.Xpath("//a[contains(text(),'myText')]"));

String myLink = anchor.getAttribute("href"); // null pointer exception
You are getting exception because you didn't set the anchor value, anchor variable is created and intialized to null and after that it is never updated.
I think the correct code should be below
String myLink = element.getAttribute("href");

Try using a WebDriver instance directly instead of WebElement instance...i.e., use driver instead of ul..
you can try with wait,
element = new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("myText")));

Related

How to click on the aside / previous Table Cell (TD) in the same row using Selenium Webdriver with Java?

I want to click on the Checkbox element which is present in the dynamic web table which has 3 static columns (CheckBox, Description, Link) and dynamic rows.
I'm able to get the exact text of the description of the check box but I'm unable to click on the check box.
Here's the script I tried to achieve my expectation but didn't work. Might be a wrong approach.
WebElement dataTable = driver.findElement(By.id("table_id"));
List<WebElement> TDs = dataTable.findElements(By.tagName("td"));
for(WebElement td : TDs)
{
if (!td.getText().trim().equals("text that i want to click on its checkbox"))
continue;
WebElement particularTd = td.findElement(By.xpath("//*[#type='checkbox']//preceding::input"));
particularTd.click();
}
Could you tell me the right way to click on the check box?
Thanks,
Karunagara Pandi G
I think the td contains only the data.
What you can try is to navigate back to the immediate parent table-row (tr) containing table-data(td). Then searching for the input 'checkbox' there.
<table>
<tr>
<td>
<center><input type='checkbox'/></center>
</td>
<td>
<span>Cell Desc</span>
</td>
<td>
<hyperlink>
</td>
</tr>
</table>
To click the checkbox you can use xpath:
//*[#text='Cell Desc']/ancestor::tr//input
To make the xpath dynamic you can fetch the value and use that value
String value = "Cell Desc";
String xpath = "//*[#text='" + value + "']/ancestor::tr//input";
This worked for me please try the below code:
for(WebElement td : TDs)
{
if (td.getText().contains("Your Text")
td.click;
}
//if you are unable to click with td.click; use below code
for(WebElement td : TDs)
{
if (td.getText().contains("Your Text")
{
WebElement particularTd = td.findElement(By.xpath("//*[#type='checkbox']//preceding::input"));
particularTd.click()
}
}

Element is not clickable at point - other element would receive the click

I have an element on a website which looks like below
<div id="wrapperCategory" class="categoryItemWrapper">
<div class="panel panel-default panel-categoryItem text-center categoryItem disabledItem" ng-class="category.CategoryStyleClass" ng-click="setselectedProduct(productIndex,product,category); setselectedProductAmount(null);" title="Category">
<div class="panel-heading">
Category
</div>
<div class="panel-body">
Price
</div>
<div class="panel-footer availability" ng-class="category.AvailabilityStyleClass">
<span ng-bind-html="category.AvailabilityText">Avail</span>
</div>
</div>
</div>
I need to click on it to get forward. If I manually click on each of these divs or on span website goes forward but SeleniumWebdriver can't click any of them.
I tried click on span and on div with ng-click event buch each time i get an error:
Exception in thread "main" org.openqa.selenium.WebDriverException: Element <div class="panel panel-default panel-categoryItem text-center categoryItem" ng-class="category.CategoryStyleClass" ng-click="setselectedProduct(productIndex,product,category); setselectedProductAmount(null);" title="Category"></div> is not clickable at point (619.2666625976562, 474.23333740234375). Other element would receive the click: <div style="top: 0;left: 0;bottom: 0;right: 0;position: fixed;pointer-events: auto !important;z-index: 10000;" id="cover-1526454140024"></div>
I don't get it.
Can I somehow check which element is clickable and which element overlaps this which I want to click (I dont'see any div with id="cover-1526454140024" in code of the website) ?
Updated:
Unfortunately it still doesn't work after your solutions.
The same exception when trying to click.
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
JavascriptExecutor js = (JavascriptExecutor) Mundial.driver;
js.executeScript("arguments[0].scrollIntoView();", categoryItem);
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(categoryItem));
categoryItem.click();
List<WebElement> selects = driver.findElements(By.tagName("select"));
Select ticketsToSelect = new Select(selects.get(3));
ticketsToSelect.selectByValue("number:2");
It only works in case when I put sleep and scroll down manually. I don't get it.
As per your response :
You will have to scroll down to let the web element available to your script.For that you can use this code :
public static void scrollDown(WebDriver driver, String YoffSet){
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript(YoffSet);
}
here you can call this method anywhere from your code.
Then you can use this code to interact with the web element :
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("static ID")));
element.click();
I experienced lot of such issues, as Ankur says, use wait before click
WebElement seems_unclickable = wait.until(ExpectedConditions.elementToBeClickable(By.id(...
One of the way is ExpectedConditions commands
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("Yourid")));
More examples can be found at http://toolsqa.com/selenium-webdriver/wait-commands/
Try "scrollIntoView".
public void ScrollElementIntoView(IWebElement element)
{
var js = driver as IJavaScriptExecutor;
js.ExecuteScript("arguments[0].scrollIntoView()", element);
}
I had the same issue and it was due to a having a non 100% zoom applied to the page body like:
body {
zoom: 90%;
}
Removing the css zoom fixed the issue.

Unable to click textarea

I'm working on a project that's coded in Java and I'm using Selenium to automate a process.
I have these two textareas and I've no problem at all to send my text to the first area because I can find it by id.
<td class="tdLeft">:</td>
<td class="tdRight">
<textarea placeholder="" id="add_links" name="links[]"></textarea>
</td>
driver.findElementById("add_links").sendKeys("\n");
But there is a second text area that I have problems with.
<tr>
<td class="tdLeft">:</td>
<td class="tdRight">
<textarea name="links[]"></textarea>
</td>
</tr>
How can I get my text into this one ?
Long way would be:
List<WebElement> txtAreas = driver.findElements(By.xpath("//textarea[name='links[]']"));
for (WebElement txtArea : txtAreas) {
if (!txtArea.getAttribute("id").equalsIgnoreCase("add_links")) {
txtArea.sendKeys("\n");
break;
}
}
Simplest way would be:
WebElement txtArea = driver.findElement(By.xpath("//textarea[name='links[]' and not(#id = 'add_links')]"));
txtArea.sendKeys("\n");
If you know textarea index you can try following way,
driver.findElementsByXPath("//textarea").get(1).sendKeys("\n");
You have 2 textareas with same values for name attribute. what you can do for second text area is as below:
List<WebElement> linksize=null;
String arraylinks[]=null;
linksize = driver.findElements(By.name("links[]"));
int linksCount = linksize.size();
arraylinks= new String[linksCount];
for(int i=0;i<linksCount;i++)
{
arraylinks[i] = linksize.get(i).getAttribute("id");
if(arraylinks[i].isEmpty())
{
System.out.println("I am second text area"+arraylinks[i]);
linksize.get(i).sendKeys("Hello");
}
}
Here we are getting text area having common attributes, looping over them and sending text only to that one which do not have id. It is working for me. Give it a try.
Try this xpath,driver.findElement(By.xpath("//tr/td[2]/textarea[#]"))
You can try by using cssSelectors:
driver.findElement(By.cssSelector("textarea[name='links[]']"));
I found doubleclick and then sendkey as solution :D
Actions action = new Actions(driver);
action.moveToElement(findElement(locator)).doubleClick().build().perform();
locator.sendKeys("Text");

Select link in a table with jsoup using Java code

I need to get the download link in this table:
<table cellpadding="0" cellspacing="3" border="0">
<tr>
<td><img class="img" src="...path" /></td>
<td>File -
<a id="1569" class="tepLink" href="javascript:void(0);">[Click me]</a>
</td>
</tr>
</table>
and this is what I tried:
Element table = doc.select("table[cellpadding=\"0\" cellspacing=\"3\" border=\"0\"]").first();
Element dwlLink = table.select("td:has(a)").first();
String absPath = dwlLink.attr("abs:href");
//use download manager to download from string absPath
I always get a "null object reference" so I must be wrong with that code, what should it do?
Just select all anchor tags and then get the first element in the Elements object.
Elements anchorTags = doc.select("table[cellpadding=0][cellspacing=3][border=0] a");
if(anchorTags.isEmpty())
{
System.out.println("Not found");
}
else
{
System.out.println(anchorTags.first());
}
EDIT:
I changed the select method to include the cellpadding, cellspacing and border attributes since that seems like what you were after in one of your examples.
Also, the Element.first() method returns null if the Elements list is empty. Always check for null when calling that method to prevent NullPointerExceptions.
table.select("td:has(a)").first(); will select the first <tr> element that contains an anchor. It will not select the anchor <a> itself.
here is what you can do:
Element aEl = doc.select("table[cellpadding] td a").first();

get href value (WebDriver)

How do I get a value from href?
like this eg:
<div id="cont"><div class="bclass1" id="idOne">Test</div>
<div id="testId"><a href="**NEED THIS VALUE AS STRING**">
<img src="img1.png" class="clasOne" />
</a>
</div>
</div>
</div>
I need that value as string.
I've tried with this:
String e = driverCE.findElement(By.xpath("//div[#id='testId']")).getAttribute("href");
JOptionPane.showMessageDialog(null, e);
But just returns NULL value...
You have pointed your element to 'div' instead of 'a'
Try the below code
driverCE.findElement(By.xpath("//div[#id='testId']/a")).getAttribute("href");
If you got more than one anchor tag, the following code snippet will help to find all the links pointed by href
//find all anchor tags in the page
List<WebElement> refList = driver.findElements(By.tagName("a"));
//iterate over web elements and use the getAttribute method to
//find the hypertext reference's value.
for(WebElement we : refList) {
System.out.println(we.getAttribute("href"));
}

Categories

Resources