unable to click element with value zero - c#

I am trying to click on the div tag that has id="0". Although it can find that id, unable to click on it. clicking on other ids work.
This was working in Firefox 12 but failing in 13
I am using Selenium2 with C#
driver.FindElement(By.XPath("//div[#class='MenuItem' and #id='0']")).Click();
<div style="position: absolute; top: 72px; left: 9px; " id="file" class="Menu">
<div class="MenuItem" id="0"> <img src="images/UI/Icons/folder.png">Folder</div>
<div class="MenuItem" id="1"> <img valign="middle" src="images/UI/Icons/tree/folderBlogClosed.png">Blog</div>
<div class="MenuItem" id="2"> <img valign="middle" src="images/UI/Icons/folderBoard.png">Discussion Board</div>
</div>

Elements with ID attributes that start with numbers are invalid HTML. While most browsers will render this just fine, it's entirely possible that attempting to use JavaScript to locate the element by the ID (as Selenium WebDriver does) will fail.

Is there a reason you're using an XPath and not just selecting by the ID?
driver.FindElement(By.Id("0"));
HTML id's should be unique on a page.

I resolved this issue by using mouse actions
. Thank you all for your input.
Actions builder = new Actions(driver);
IWebElement tagElement = driver.FindElement(By.XPath("//div[#id='0' and #class='MenuItem']"));
//mouse hover on first item
builder.MoveToElement(tagElement).Perform();
//click on first item
driver.FindElement(By.XPath("//div[#id='0' and #class='MenuItem']")).Click();

Related

How to SendKeys to a div element with coleditable="true" using Selenium and C#

There is an element I can't figure out how to type text into it.
<div class="floatstart gridcell grideditablefield" colname="Items" coltype="string" coleditable="true" val="" style="width: 107px; overflow: hidden; cursor: text; height: 100%;"> </div>
Without code (manually) in order to put text in it I need to click on it twice, so in the code If I click on the element once, the class seems to change to:
<div class="floatstart gridcell grideditablefield activecell"...> </div>
And when I click on the element again it changes once more to
<div class="floatstart gridcell grideditablefield activecell NDColEditableInEdit"...> </div>
Anyway, once I try to send keys to it I get an Error:
OpenQA.Selenium.ElementNotVisibleException: 'element not interactable
(Session info: chrome=//doesn't matter)
(Driver info: chromedriver=//doesn't matter (6a5d10861ce8de5fce22564658033b43cb7de047-refs/branch-heads/4896#{#875}),platform=Windows NT 10.0.19042 x86_64)'
Code:
// It does find the right element
var element = driverReUse.FindElementByXPath("//*[#id='CalcOfAmmountsData']/div/div[2]/div/div[1]/div[2]");
element.Click();
element.Click();
element.SendKeys("keys");
Please help
Try the below,
Actions act = new Actions(driver);
act.moveToElement(driver.findElement(By.xpath("//[#id='CalcOfAmmountsData']/div/div[2]/div/div[1]/div[2]"))).doubleClick().build().perform();
act.sendKeys('Keys');
Please make the changed if any required for c#, in above.
Hope this answer your question.
Generally <div> tags are not interactable unless it contains the attribute contenteditable="true".
Deep Dive
As you mentioned, initially the <div> element is:
<div class="floatstart gridcell grideditablefield" colname="Items" coltype="string" coleditable="true" ...> </div>
After first click:
<div class="floatstart gridcell grideditablefield activecell" colname="Items" coltype="string" coleditable="true" ...> </div>
After second click:
<div class="floatstart gridcell grideditablefield activecell NDColEditableInEdit" colname="Items" coltype="string" coleditable="true" ...> </div>
As per this and this discussion either after the first or second click an <input> / <textarea> gets added, where you need to send the character sequence as follows:
// It does find the right element
var element = driverReUse.FindElementByXPath("//*[#id='CalcOfAmmountsData']/div/div[2]/div/div[1]/div[2]");
element.Click();
element.Click();
var input = driverReUse.FindElementByXPath("xpath_input_textarea");
input.SendKeys("keys");

I cant Locate the Xpath of Pseudo Comment

I just want to know how to get the Xpath Of pseudo Element Actually the thing is that in This I want to .CLick() on this Div Its role Is button so i tried just To xpath with Div Class name it is locating the element but Not clicking i have to Click On this ::after But i cant Get the Xpath Because it is a Pseudo Element
<div id=":l6" class="T-I J-J5-Ji amD T-I-awG T-I-ax7 T-I-Js-Gs L3"
role="button" tabindex="0"
data-tooltip="Older"
aria-label="Older"
style="user-select: none;"
xpath="1">
<span class="amF"
aria-hidden="true">
</span>
<img class="amJ T-I-J3"
src="images/cleardot.gif" alt="">
::after
</div>
What I have read "The Selenium API doesn't support pseudo-elements".
So you can use try using Actions.
I have a sample (I don't have the full path you can add) here with CssSelector:
Actions action = new Actions(Session.Driver);
action.MoveToElement(FindElement(By.CssSelector("div.img::after"))).ContextClick().Build().Perform();
As a last resort, you can use JavaScript with ExecuteScript something like this:
var afterElem = driver.FindElement(By.CssSelector("div.img"));
var buttonAfter = driver.ExecuteScript(
"return window.getComputedStyle(arguments[0],'::after');"
, afterElem);
buttonAfter.click();
You will need to play with the CssSelector Or use an XPath.
Hope this helps you!

Webdriver Selenium different behavior on different computers

I wrote some code to do repetitive tasks with the Webdriver in C#.
My code works well on my PC but it is grabbing a different element on my boss's PC.
The element I'm looking for:
var InventMgmt = driver.FindElement(By.XPath("//*[#id='ROOT/Inventory Management']/div/span"));
This element is from a WebApp my company is using and it's the same across the network.
The problem is that the Webdriver is clicking a completely different element that is not listed in my code.
Both PCs are using the same Corporate OS (Windows 10), with the same browser versions.
After reviewing my code and Inspecting Element in my boss's browser, I'm starting to believe the issue can only be coming from my boss's PC...what could cause this kind of issue?
Here's the innerHTML of the elements I'm looking for and the one that are actually being clicked:
Trying to click the "Inventory Management" span:
<div id="ROOT/Inventory Management" class="x-panel x-panel-noborder x-tree x-panel-collapsed" style="width: auto;">
<div class="x-panel-header x-panel-header-noborder x-unselectable x-accordion-hd" id="ext-gen186" tabindex="0" role="tab" aria-expanded="false" style="cursor: pointer;">
<span class="x-panel-header-text" id="ext-gen190">Inventory Management</span>
</div>
</div>
Element being clicked is the div with id "ROOT/Support Table":
<div id="ROOT/Support Table" class="x-panel x-panel-noborder x-tree" style="width: auto;">
<div class="x-panel-header x-panel-header-noborder x-unselectable x-accordion-hd" id="ext-gen248" tabindex="0" role="tab" aria-expanded="true" style="cursor: pointer;">
<span class="x-panel-header-text" id="ext-gen252">Support Table</span>
</div>
</div>
You may want to check your browser 'Zoom Level'. It should be 100% for your automation to work as expected. I ran into a similar situation once.
I recommend restricting the driver from starting if zoom levels are not 100.
InternetExplorerOptions ieOptions = new InternetExplorerOptions();
ieOptions.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
ieOptions.IgnoreZoomLevel = false;
IWebDriver driver = new InternetExplorerDriver(IEDriverFolder, ieOptions);

How to find element with non-static ID using Selenium Webdriver C#

I want to be able to send text to this text box element:
<form class="compose okform initialized">
<div class="border"></div>
<div id="message_9028519832635440005Container" class="inputcontainer textarea empty">
<textarea class="clone" placeholder="Compose your message" aria-hidden="true" style="height: 21px; width: 417px; line-height: 18px; text-decoration: none; letter-spacing: 0px;" tabindex="-1"></textarea>
<textarea id="message_9028519832635440005" placeholder="Compose your message" style="height: 39px;"></textarea>
<span class="okform-feedback message empty" style="height: 0"></span>
<div class="icon okicon"></div>
</div>
<button class="flatbutton" type="submit"></button>
<div class="draft_message"></div>
<label class="checkbox" for="enter_to_send_9028519832635440005"></label>
</form>
I'm not able to location the element by searching for part of the ID (the number after message_ is dynamically generated).
I've also tried this but I get an error saying "unknown error: cannot focus element":
var textBox = DriverActions.driver.FindElements(By.ClassName("inputcontainer"));
textBox[0].SendKeys("Why hello");
Try the following css
.inputcontainer.textarea.empty>textarea:nth-child(1)
I assumed you want the first text area box with placeholder="Compose your message"
if so, you can also use the following cssSelector
[placeholder='Compose your message'][class='clone']
Partial search with id is also possible. Assuming the Container part of the div's id is unique and static, you can do the following
[id$='Container']>textarea:nth-child(1)
On the other hand, if you want the second textarea just simply change the child index
[id$='Container']>textarea:nth-child(2)
And, here is the implementation
By byCss = By.CssSelector("[id$='Container']>textarea:nth-child(1)");
IWebElement textBox = driver.FindElement(byCss);
textBox.SendKeys("Why hello");
Try this XPath using starts-with
var textBox= DriverActions.driver.FindElement(By.XPath(".//textarea[starts-with(#id,'message_')]"));
textBox.SendKeys("Why hello");
The reason that you are getting the error could be because by your selector you will end up getting a div instead of a textBox that you need.

Selenium webdriver c# - unable to click() element (element not null)

I seem to be having issues clicking a element inside a box which is filled by ajax.
So the web page I am on has a link on it which when clicked this calls a javascript function which then inserts a new div into the page which is full of new content.
Now the weird thing is I can find the element inside this box no problem using xpath, and I can even read its value but! I can't use Click(); on the link inside the box the event just wont work for some reason.
Has anyone faced a similar issue and know a work around?
I am using Selenium webdriver 2.35 with Firefox 23
More Info
OK so the HTML for the link I click which calls the JS to make the div appear.
<center>
<a id="link_fleet_move_here" href="">Move fleet here</a>
</center>
<br>
<script>
$("#link_fleet_move_here").click( function(event) { event.preventDefault(); load_fleet_move_to_destination("fleet.aspx?method=ajax&view=move_to_destination&version=1&player=111&destination=LZLOCATION"); $("#link_fleet_move_here").hide();} )
</script>
<center>
<div id="fleetLoaderTemplate" style="display:none">
<div id="fleetLoaderErrorTemplate" style="display:none">
</center>
<div id="move_to_destination_container"></div>
And when the event finishes loading the new HTML
<div id="move_to_destination_container">
<ajax>
<table width="600" align="center">
BIG TABLE FULL OF CONTENT
<td sorttable_customkey="LZLOCATION">
(LZLOCATION)
</td>
<td sorttable_customkey=""></td>
<td sorttable_customkey=""></td>
<td>
Move
</td>
<table>
<br>
</ajax>
</div>
The Selector
location = driver.FindElement(By.XPath("//a[contains(#href, '" + LZLocation + "')]/following::td[3]"));
location.Click();
I think it may be something to do with that div actually, I think it starts as Display:None and gets changed, this will be effecting it?
I thought it was dynamically adding it but maybe not!
Try selecting your element via the following:
driver.findElement(By.cssSelector("#move_to_destination_container a[href^='fleet']")).click();
If it throws an error, try using:
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#move_to_destination_container a[href^='fleet']"))).click();

Categories

Resources