How to deal with div tag elements using Selenium

1,507 views
Skip to first unread message

Uday vangala

unread,
Jul 23, 2015, 2:02:07 AM7/23/15
to Selenium Users
Hi,

I am new to selenium. 

I am unable to select value in drop down which are codded with <div> tag. ( Problem is not only with drop down, any thing which are coded in div )

For Ref: Create Your Google Account  Gender/Birthday drop downs
 

<div title="Gender" aria-activedescendant=":d" aria-haspopup="true" tabindex="0" aria-expanded="false" style="-moz-user-select: none;" role="listbox" class="goog-inline-block goog-flat-menu-button jfk-select"><div aria-posinset="0" aria-setsize="3" role="option" id=":d" class="goog-inline-block goog-flat-menu-button-caption">I am...</div><div aria-hidden="true" class="goog-inline-block goog-flat-menu-button-dropdown">&nbsp;</div></div><input id="HiddenGender" name="Gender" type="hidden">

Ashish Thakur

unread,
Jul 23, 2015, 1:28:20 PM7/23/15
to seleniu...@googlegroups.com
You cant use sendkeys on this
You will have to use xpath of the element and click on it
Each option will furhur have an xpath on which you can click

Regards
Ashish
http://qtpselenium.com

Regards
Ashish Thakur
Whizdom Trainings

US Contact Number: +1(917)-745-8787
India Contact Number: (+91) 8968585110


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/9206606a-9aea-47e7-9c7f-904f096dcd06%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kaleem Uddin Mohammed Abdul

unread,
Jul 26, 2015, 1:27:25 AM7/26/15
to Selenium Users, udayva...@gmail.com
Unfortunately here the Sendkeys are not working as it is contextual menu. The below is working fine.
_WebDriver=new FirefoxDriver();
         _WebDriver.navigate().to("http://www.gmail.com");
         _WebDriver.manage().window().maximize();
         Thread.sleep(2000);
         _WebDriver.findElement(By.xpath("//*[@id='link-signup']/a")).click();
         Thread.sleep(4000);
         _WebDriver.findElement(By.xpath("//*[@id='BirthMonth']/div/div[2]")).click();
         Thread.sleep(2000);
         List<WebElement>ListOfMonths=_WebDriver.findElements(By.xpath("//*[@class='goog-menuitem-content']"));
         JavascriptExecutor JSE=(JavascriptExecutor) _WebDriver;
         for(WebElement Month : ListOfMonths)
         {
             System.out.println(Month.getText());
             if(Month.getText().equalsIgnoreCase("August"))
             {
         JSE.executeScript("arguments[0].scrollIntoView(true)", Month);
         Month.click();
         break;

Mercious

unread,
Jul 27, 2015, 3:29:40 AM7/27/15
to Selenium Users, udayva...@gmail.com, makale...@gmail.com
In this context, i really wish Selenium and the drivers would implement something that allows to search something like By.attributeWithNameEquals("role","option");.

Something along these lines. 

There shouldn't be any technical difference between that and id?

Jim Evans

unread,
Jul 27, 2015, 5:27:45 AM7/27/15
to Selenium Users
Actually, with the exception of the link text locators, all of the other locator methods translate directly into functions available in JavaScript. To wit:

id = document.getElementById()
name = document.getElementsByName()
class name = document.getElementsByClassName()
tag name = document.getElementsByTagName()
CSS selector = document.querySelectot()
XPath = document.evaluate() - though of course not implemented by all browsers, IE being the notable exception.

However, as I'm sure you've figured out, most of these DOM functions are obsolete, and can be expressed directly as a CSS selector. Indeed, this is why the W3C WebDriver specification does not define most of these locators as separate entities. Furthermore, a By.attributeNameWithValue is trivial to write.

findElement(By.cssSelector("*[" + name + "='" + value + "']"));

Mercious

unread,
Jul 28, 2015, 10:16:04 AM7/28/15
to Selenium Users, james.h....@gmail.com
Wasn't familiar with css-selectors indeed.

Then is byId implemented basically as findElement(By.cssSelector("*["id='" + value + "']")); (or anything that is logically equivalent)?

Jim Evans

unread,
Jul 28, 2015, 10:27:33 AM7/28/15
to Selenium Users
Yes, that would work, but CSS selectors have a shortcut for the id attribute. The selector "#foo" will find element with the id attribute with the value 'foo'.

Mercious

unread,
Jul 28, 2015, 11:25:39 AM7/28/15
to Selenium Users, james.h....@gmail.com
Tested it and CSS-Selectors are indeed pretty sexy. 

Thanks for the hint. 
Reply all
Reply to author
Forward
0 new messages