Select a text inside a Div

517 views
Skip to first unread message

SeleniumMember

unread,
Jul 1, 2015, 9:25:35 AM7/1/15
to seleniu...@googlegroups.com
Hi all,

I am very new to CSS and I am trying to select a Text inside the div using CSS locator. I have tried bunch of stuff and goggled a lot as well, I can't seem to get this right. I will appreciate any help.

1. Div class="category">Dashboards One</div> 

   Div class="category">Dashboard Two</div>

   Div class="category">Dashboards Three</div>


div.category but I don't know how to get Text.

.category') div('text') - Wrong

div[class='category'] and .text() ='Dashboards One'



2. <li class=""category"> <Div> Settings One</div><li/>

    <li class=""category"> <Div> Settings Two</div><li/>

    <li class=""category"> <Div> Settings Three</div><li/>



Thanks



derekmw

unread,
Jul 1, 2015, 9:41:37 AM7/1/15
to seleniu...@googlegroups.com
Have you tried this:

div:contains('Dashboards One')

SeleniumMember

unread,
Jul 1, 2015, 10:04:36 AM7/1/15
to seleniu...@googlegroups.com
I remember contains was removed from css a while ago. So, it doesn't work.

Sonia saini

unread,
Jul 1, 2015, 10:15:18 AM7/1/15
to seleniu...@googlegroups.com
Try this:

First you see in chrome developer tool are you getting text or not with below css? if not then modify little bit.

$(". category > li").text();

--
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/2992dfff-57d8-4976-8d2b-7e0f008a51f8%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Sonia saini

SeleniumMember

unread,
Jul 1, 2015, 12:30:36 PM7/1/15
to seleniu...@googlegroups.com
I tried this JQuery in Chrome $(". category > li").text();
Result - ""

I changed it a bit and tried few things, either I get "" or [].

Doug Dragon

unread,
Jul 1, 2015, 2:15:44 PM7/1/15
to seleniu...@googlegroups.com

In python you can use ".text":

first_div_text = browser.find_element_by_css_selector("div.class").text

Hope that helps,

-Doug

SeleniumMember

unread,
Jul 2, 2015, 9:54:08 AM7/2/15
to seleniu...@googlegroups.com
The JQuery ('.category:contains("Dashboards One")') worked however, since there is no contains in CSS, still no luck yet.

Thank you


On Wednesday, July 1, 2015 at 8:25:35 AM UTC-5, SeleniumMember wrote:

Shawn McCarthy

unread,
Jul 2, 2015, 11:19:08 AM7/2/15
to seleniu...@googlegroups.com
To find an element by it's text, I use xpath.

SeleniumMember

unread,
Jul 2, 2015, 11:25:30 AM7/2/15
to seleniu...@googlegroups.com
Our Products support IE, FF, and Chrome. Since xpath is not supported in IE, I am trying to use css selector for all. I would use xpath if it worked well in IE as I am familiar with it.

Is there really no way to select a text inside a div using CSS selector ?


On Wednesday, July 1, 2015 at 8:25:35 AM UTC-5, SeleniumMember wrote:

Doug Dragon

unread,
Jul 2, 2015, 11:44:19 AM7/2/15
to seleniu...@googlegroups.com
You should be able to still use the ".text" I mentioned above. Something like this works just fine for me:

from selenium import webdriver
browser
= webdriver.Firefox()
div_elements
= browser.find_elements_by_css_selector("div.category")
for element in div_elements:
  if "Dashboards One" in element.text:
    # Do something
   
print("Found 'Dashboards One")
   break


-Doug

derekmw

unread,
Jul 2, 2015, 2:04:07 PM7/2/15
to seleniu...@googlegroups.com
have you tried xpath?  We use xpath for a lot of automation against all 3 browsers and rarely find any issues where it doesn't work across all 3 (including IE).

SeleniumMember

unread,
Jul 2, 2015, 2:17:30 PM7/2/15
to seleniu...@googlegroups.com
Yes, in the past I used xpath with Selenium RC. xpath was super slow on IE and tests never finished on IE. Because of that I am trying to use css selector with Selenium WebDriver. Unless things have changed with WebDriver and xpath in IE.


On Wednesday, July 1, 2015 at 8:25:35 AM UTC-5, SeleniumMember wrote:

derekmw

unread,
Jul 2, 2015, 2:24:11 PM7/2/15
to seleniu...@googlegroups.com
yes, xpath is slower but I wouldn't say that slow.  We try to use CSS where possible for speed but use xpath when it makes sense - for example we have a radio button that has dynamic id (and no other attributes).  So we locate this through xpath using the label, and then traverse to the 'yes'/'no' selection. (like - //label[contains(text(), 'Have this been verified')]/..//*[text()='No']

I would at least try xpath for cases like this and see how the speed is.  You may be pleasantly surprised.

Oscar Rieken

unread,
Jul 2, 2015, 2:58:24 PM7/2/15
to seleniu...@googlegroups.com
I think the simplest solution would be to talk to your dev team and ask them to add an id to those items, or checkout the code and add them yourself.



--
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.

SeleniumMember

unread,
Jul 13, 2015, 11:18:57 AM7/13/15
to seleniu...@googlegroups.com
Thank you all for your responses. I have decided to use css and xpath mix and match for now. I did notice that whether its css or xpath, Selenium test runs super slow in IE. I am not sure if anyone else has run into this issue as well.

Thanks


On Wednesday, July 1, 2015 at 8:25:35 AM UTC-5, SeleniumMember wrote:

Jim Evans

unread,
Jul 13, 2015, 11:29:34 AM7/13/15
to seleniu...@googlegroups.com
Assuming you're using WebDriver, what do you mean by "super slow?" What version(s) of Internet Explorer? All? Just one? What version of Selenium are you using? Is it every WebDriver operation, or just a few, like sendKeys? Are you using 32-bit or 64-bit Windows? What IEDriverServer.exe are you using (version *and* 32-bit vs. 64-bit)? I reject outright the notion that IE is "just slow" without further supporting evidence.

Anjana Manandhar

unread,
Jul 13, 2015, 12:00:42 PM7/13/15
to seleniu...@googlegroups.com
So, far I have tested only IE version 11.
I am using Selenium "selenium-server-standalone-2.45.0". I noticed the slowness mainly in the sendKeys. I haven't done a lot of testing with IE11 after seeing this.
Windows 7 - 64-bit
IEDriverServer - 64-bit

Thanks

--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/PxNIzzEHDG0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.

Jim Evans

unread,
Jul 13, 2015, 1:16:02 PM7/13/15
to seleniu...@googlegroups.com
I suspected as much. Keystrokes are very slow in IE10 and 11 if you're using the 64-bit IEDriverServer.exe. The workaround is to use the 32-bit server. For a full technical discussion of why this is so, and why there's not going to be a "fix" for this issue, please see http://jimevansmusic.blogspot.com/2014/09/screenshots-sendkeys-and-sixty-four.html

SeleniumMember

unread,
Jul 15, 2015, 8:52:48 AM7/15/15
to seleniu...@googlegroups.com
Thanks Jim for the workaround. I tried that 32-bit driver as you suggested, my test logins to application way faster than the 64-bit driver, however it wont find the element. So, I am stuck.

On Wednesday, July 1, 2015 at 8:25:35 AM UTC-5, SeleniumMember wrote:

Milan Yadav

unread,
Jul 27, 2015, 9:01:15 AM7/27/15
to Selenium Users, anjana.m...@gmail.com
Try  $(".category").textContent in console.
Reply all
Reply to author
Forward
0 new messages