1

I'm new to web development and I'm experimenting with Selenium in Python.

I want to find the id of the child below, given that I only know class="entry-content" as its parent.

Example html:

<div class="entry-content">
  <div id="pl-154">
    ...
  </div>
</div>

Example output:

pl-154

Thank you beforehand!

Update: Thank you all for the comments! However when I tried the code on the website I'm working with, it doesn't work and I'm not sure why. Code

chocolatte
  • 352
  • 4
  • 15

3 Answers3

2

Since you are looking for child you can use CSS SELECTOR or XPATH. I would recommend cssselector.

CSS SELECTOR : If you have multiple div's with id inside <div class="entry-content">

div[class='entry-content']>div

The above css selector will give you all the divs which are inside <div class="entry-content">

you can try something like div[class='entry-content']+div

If you are looking for xpath it'd be like this :

//div[@class='entry-content']/div or //div[@class='entry-content']/child::div

Whatever you are using, make sure to use get_attribute(attribute name) to get the attribute value.

div_id_value = driver.find_element_by_xpath("//div[@class='entry-content']/div").get_attribute('id')
print(div_id_value)
   

CSS SELECTOR REFRENCE

XPATH REFRENCE

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • @chocolatte : Share that html in text format along with error stack trace. – cruisepandey May 06 '21 at 14:14
  • 1
    @chocolatte : Actually you can try with `div_id_value = driver.find_element_by_xpath("//div[@class='entry-content']/div[@class='panel-layout']").get_attribute('id') print(div_id_value)` – cruisepandey May 06 '21 at 14:27
  • If you don't mind me asking, why didn't `div_id_value = driver.find_element_by_xpath("//div[@class='entry-content']/div").get_attribute('id') ` work in the first place? There's no error but the `div_id_value` was empty – chocolatte May 06 '21 at 15:05
  • 1
    @chocolatte : that could be because `//div[@class='entry-content']/div` might have multiple entries in DOM. Suggestion go to dev tool and check for the mentioned xpath and see how many entries are present. – cruisepandey May 06 '21 at 15:26
  • I attached a picture of `
    ` in my post, and there are 3 children divs inside it. So I just want to make sure, the original code only works if there's 1 child div, is that correct?
    – chocolatte May 06 '21 at 17:44
  • 1
    @chocolatte : Yes correct. Now you have 3 child's so you will have to distinguish which one to select, if not declared only first child should get preference – cruisepandey May 06 '21 at 19:24
1

You can use the ./child::* expression with xpath

element = driver.find_element_by_xpath("//div[@class='entry-content']")
child = element.find_element_by_xpath("./child::*")
idVal = child.get_attribute('id'))

Your id value should be stored in the idVal variable
I'm not sure which version of Python you are using but this should work for Python3

booshwayne
  • 13
  • 3
  • Thank you! The first line works but in the second line, the child was empty. Does it only work if there's only one child? – chocolatte May 06 '21 at 05:19
1

Below code will give you ID of the child object

id_Value = driver.find_element_by_xpath("//div[@class='entry-content']/div").get_attribute('id'))
Rajan Domala
  • 133
  • 1
  • 8