Cannot click on the element ..not able to identify it - selenium

<div class="navpage-header-content">
<form action="textsearch.do" role="search" method="GET" class="form-inline navpage-global-search ng-non-bindable" aria-label="Global Search" target="gsft_main">
<input name="sysparm_ck" id="sysparm_ck" type="hidden" value="052ab09a0fa90700fa38563be1050e0fea31866160e7a5e6e0fc925775df282f070903d6"><div class="input-group-transparent">
<input name="sysparm_search" id="sysparm_search" placeholder="Search" type="search" class="form-control form-control-search">
<label for="sysparm_search" title="" data-original-title="Search">
<span class="input-group-addon-transparent icon-search sysparm-search-icon"></span>
</label></div></form></div>
This is the HTML tag for the element.
WebElement ele1 = driver.findElement(By.xpath("//span[#class='input-group-addon-transparent icon-search sysparm-search-icon']"));
ele1.click();
But my script cannot locate the element and click it.
I tried actions , Java script executor , but I just cannot Click on the element.

Actually you are trying to locate the span, You should locate input tag to click on search textbox, it should be like this:
WebElement SearchBox=driver.findElement(By.id("sysparm_search"));
SearchBox.click();
You can also use the xpaths to locate it:
//*[#id='sysparm_search']
//input[#id='sysparm_search']
//input[#name='sysparm_search']
Use this code for xpath:
WebElement SearchBox=driver.findElement(By.xpath("//*[#id='sysparm_search']"));
SearchBox.click();
You can also use name to locate it
Use this code for name:
WebElement SearchBox=driver.findElement(By.name("sysparm_search"));
SearchBox.click();

Related

In Selenium Webdriver using Java should I have to use element.click() before element.SendKeys for input text box?

driver.get("<url>")
WebElement name = driver.findElement(By.id(..));
name.click();
name.sendKeys("<input text>");
In the above code is it required to have name.click() first , or is it optional as the field is located and it should be able to send the input without having to click on it first
<input> tag
The <input> tag specifies an input field where the user can enter data. The <input> element can be displayed in different ways, depending on the type attribute. As an example:
<input type="text"> (default value)
<input type="button">
<input type="checkbox">
<input type="email">
<input type="file">
<input type="hidden">
<input type="number">
<input type="password">
<input type="radio">
<input type="submit">
<input type="url">
This usecase
As your usecase involves sending a character sequence presumably it should be of the following type:
<input type="text">
Generally you won't have to invoke click() before invoking sendKeys() but ideally to send a character sequence you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("birtviewer"))).sendKeys("Richard Bernard");
However, incase the element is a dynamic element having onfocus event attribute which fires the moment that the element gets focus as follows:
<input type="text" id="fname" onfocus="myFunction(this.id)">
you may require the additional step to click within the element.

Unable to find element in a form class or div class

I am unable to find an element in a form class or div class:
//Tried this clicking on Source name.
driver.findElement(By.id("__BVID__62")).click();
//Typing into Source name.
driver.findElement(By.id("__BVID__63")).sendKeys(new String[]{"CNN"});
I tried this:
driver.findElement(By.xpath("//input[contains(#class='form-control') and type='text']")).sendKeys("CNN");
HTML code:
<div class="card-body">
<fieldset id="_BVID__62" role="group" aria-labelledby="__BVID__62__BV_label" class="b-form-group form-group">
<legend id="_BVID__62__BV_label" class="col-form-label pt-0">Source Name</legend>
<div role="group" aria-labelledby="_BVID__62__BV_label" class="">
<input id="__BVID__63" type="text" class="form-control">
use this code :
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement input = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='card-body']/descendant::div/input[contains(#id,'BVID')]")));
input.sendKeys("CNN");
As per the HTML you have shared the desired element is having a dynamic ID. So to send text to the <input> node you have to induce WebDriverWait as follows :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='form-control' and starts-with(#id,'__BVID__') and #type='text']"))).sendKeys("CNN");

leniuNot able to enter value in text box of form in selenium webdriver

Hi I am using way2sms website and in send sms screen i am not able to enter the mobile number in mobile number field using selenium webdriver.
HTML Code: `
form id="smsFrm" name="smsFrm" method="post">
<input id="ssaction" type="hidden" name="ssaction" value="ss"/>
<input id="Token" type="hidden" name="Token" value="F448FDFD10E9288F9B4A204EF40EB29A.w803"/>
<div id="smilebox" style=" display:none;">
<div class="Sms fl">
<label>Mobile Number</label>
<div class="m91">
<span>+91</span>
<input id="mobile" type="text" onchange="javascript:dispLocMob(this);" onkeydown="javascript:dispLocMob(this);" onkeyup="javascript:dispLocMob(this);" value="" maxlength="10" placeholder="Mobile Number" name="mobile"/>
</div>`
Selenium Code :
`obj.findElement(By.xpath("//*[#id='sendSMS']/a")).click();
Thread.sleep(5000);
//obj.findElement(By.id("mobile")).sendKeys("8186867724");
obj.findElement(By.xpath("//*[#id='mobile']")).sendKeys("1234567890");`
Your element has an id, which is always the best way to address an HTML element using Selenium - very simple but still absolutely unique (bc otherwise it wouldn't be valid HTML). So try:
findElement(By.id("username"))
Full C# sample (should be similar in Java):
static void Main()
{
var driver = new ChromeDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(15));
driver.Navigate().GoToUrl("http://site23.way2sms.com/content/index.html");
IWebElement element = driver.FindElement(By.Id("username"));
element.SendKeys("000");
}
I switched to the frame and got it detected.
driver.switchTo().frame(driver.findElement(By.id("frame")));
driver.findElement(By.id("mobile")).sendKeys("123456");

Check 'checkBox' in another way -> xPath

I'm using this code:
//input[#type='checkbox'][following-sibling::text()[position()=1]][2]
for checking boxes, but i want to use string, not number (example: 'Uczyń odpowiedzialnym' instead of [2]).
I tried this, but it doesn't work:
//input[#type='checkbox'][following-sibling::text()[position()=1][contains(., 'Uczyń odpowiedzialnym')]]
HTML code:
<input class="prettifiedIeCheckbox ng-pristine ng-valid" type="checkbox" ng-model="holdersModel.OnlyForRead"></input>
<label>
<span class="bottom">
Tylko do wglądu
</span>
</label>
<input class="prettifiedIeCheckbox ng-pristine ng-valid" type="checkbox" ng-model="holdersModel.ChangeMainHolder"></input>
<label>
<span class="bottom">
Uczyń odpowiedzialnym
</span>
</label>
#sideshowbarker help ;)
You need to check the span element's text in the following label sibling node:
//input[#type='checkbox' and following-sibling::label/span = 'Uczyń odpowiedzialnym']
There are, certainly, other ways to locate the element. For instance, check ng-model:
//input[contains(#ng-model, 'ChangeMainHolder')]
You can also use the below xpath:
//span[contains(.,'Uczyn odpowiedzialnym')]/preceding::input[1]
This will select the first input element that is present before the span element with exact innerHTML/text as Uczyń odpowiedzialnym.
I found another (better) solution on sqa.stackexchange by #Sam Woods, modified for my problem:
IWebElement element = driver.findElement(By.xpath("//input[#type='checkbox' and contains(#ng-model, 'ChangeMainHolder')]"));
if (!element.Selected)
{
element.Click();
}

How to click on the Browse Button

I need to upload a file, but prior to that, I need to click on the Browse button. I have tried every possible way that I can think of, but no luck. I get an error "unable to locate element".
This is the page HTML
<div class="file_input_div">
<input type="button" value="Browse" class="file_input_button">
<input type="file" style="width:100px;" onchange="javascript: document.getElementById('fileName').value = this.value" value="" size="25" name="theFile">
My code:
WebDriverWait wait = new WebDriverWait(driver, 10);
//wait.until(ExpectedConditions.presenceOfElementLocated(By.name("theFile")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("theFile")));
driver.findElement(By.name("theFile")).click();