Xpath not working as expected while running the test - selenium

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.

Related

Invalid xpath expression to find a slider element

Very new to Katalon Studio and selenium. Trying to write an automated test using selenium webdriver to change the value of the slider on the webpage.
I am failing to locate the element. Somehting is wrong with my findelement statement. Also once I locate the slider element successfully I don't know how to change the value of the slider. Can you help? or provide some quidance?
<div class="bdr-slider"
<span id="bdrText">bdr Amount</span>
<input min="0" max="15" step"0.5" value="5" type="range">
</div>
WebElement Slider = driver.findElement(By.xpath("*[div(#class,'bdr- slider')]"))
Try this :
WebElement Slider = driver.findElement(By.xpath("//div[#class='bdr-slider']"))
I'm not familiar with specifics of mobile automation, but I think that you need to locate the input element.
This one
<input min="0" max="15" step"0.5" value="5" type="range">
and then maybe call sendKeys method on that element.
You can try this line of code:
driver.findElement(By.xpath("//div[#class='bdr-slider']/input")).sendKeys("10");
Before proceeding, make sure the element is really present in the DOM, and thus making sure the problem is not with the path, enter $x('//div[#class="bdr-slider"]/input') to console in DevTools and see if you can locate the element.
If you can find the element, since you are using Katalon Studio, you can do this:
TestObject slider = new TestObject().addProperty('css', ConditionType.EQUALS, '.bdr-slider input')
or, if you prefer xpath:
TestObject slider = new TestObject().addProperty('xpath', ConditionType.EQUALS, "//div[#class='bdr-slider']/input")
You will need to import these two:
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject as TestObject

How to locate an element with div format in Selenium. getting error Unable to locate an element with the xpath expression

Below is the code :
<div class="footer-bottom-left">
<div class="campaignUser">Campaign User</div>
<div class="callLogLookUp">Call Log Look Up</div>
</div>
I have tried the below code in selenium:
driver.findElement(By.xpath(".//div[#class='footer-bottom-left'].//div[#class='callLogLookUp']")).click();`
You missed dash in class name. Also you need to remove second dot (first is also not required). So instead of
.//div[#class='footer-bottomleft'].//div[#class='callLogLookUp']
try
//div[#class='footer-bottom-left']//div[#class='callLogLookUp']
You might also need to implement ExplicitWait:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='footer-bottom-left']//div[#class='callLogLookUp']"))).click();

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);

Find element by XPath isn't working in selenium

I'm trying to find a element in selenium with this XPATH /html/body/div[3]/div/div/div[2]/div[2]/div/div/div[1]/form/div/input.
I copied it from inspect element, and copy xpath. I saw that some persons with the same problem use "*" character but I don't know where I should to use it.
this the html code
<input type="text" data-bind="value: CorpItem.Name, valueUpdate: 'afterkeydown'"
class="form-control" placeholder="Enter ..." required="required">
Here is my code on Selenium
IWebElement corpName = driver.FindElement(By.CssSelector("/html/body/div[3]/div/div/div[2]/div[2]/div/div/div[1]/form/div/input"))
Try this XPath
//input[#type='text'][#class='form-control']
In your code you used XPath and gave selector as CSS. Please verify.
Using your XPath, your code must be
IWebElement corpName = driver.FindElement(By.XPath("/html/body/div[3]/div/div/div[2]/div[2]/div/div/div[1]/form/div/input"))
If it didn't work, try
IWebElement corpName = driver.FindElement(By.XPath("//input[#type='text'][#class='form-control']"));

selenium and clicking an "a" with href=javascript

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()