1
<div class="j-C j-C-yj" style="max-height: none; -moz-user-select: none; visibility: visible; left: 218px; top: 105px; display: none;" role="menu" aria-haspopup="true">
    <div id=":1r" class="j-qn" style="-moz-user-select: none;" role="separator"></div>
    <div id=":1u" class="j-qn" style="-moz-user-select: none;" role="separator"></div>
</div>

so far i am creating xpath to select element with id=":1r" is

"(//div[contains(@class,'j-C') and contains(@class ,'j-C-yj')]/div)[1])"

i had also tried

"(//div[contains(@class,'j-C') and contains(@class ,'j-C-yj')]/div)[1])"

but none is working please help !

P.S : I cannot find element with id as id of the page is created dynamically

2 Answers 2

1

Just use xpath('.//div[contains(@class, "j-C") and contains(@class, "j-C-yj")]') as another answer already showed you.

Another Updates:

As OP kept changing the constraints of the question, here is the full solution does exactly what OP eants.

Sample: I'm using lxml to parse your string and do the xpath

from lxml import etree

s = '''<div class="j-C j-C-yj" style="max-height: none; -moz-user-select: none; visibility: visible; left: 218px; top: 105px; display: none;" role="menu" aria-haspopup="true">
    ...:     <div id=":1r" class="j-qn" style="-moz-user-select: none;" role="separator"></div>
    ...:     <div id=":1u" class="j-qn" style="-moz-user-select: none;" role="separator"></div>
    ...: </div>'''

# I need to wrap your string with <root> element otherwise first div will become the root
tree = etree.fromstring('<root>'+s+'</root>')

# xpath always returns a list, so just loop through the list and the first element is what you want
for node in tree.xpath('.//div[contains(@class, "j-C") and contains(@class, "j-C-yj")]'):
    print etree.tostring(node[0])
<div id=":1r" class="j-qn" style="-moz-user-select: none;" role="separator"/>
Sign up to request clarification or add additional context in comments.

9 Comments

the id changes every time so i can not use id to locate the element
@SalmanHasni, I've updated my answer to retrieve nodes by class.
Yes the class with remain the same
@SalmanHasni, so basically you don't really need to check the parent div, just iterate all divs with j-qn class you will get what you want by then checking the node's id
@SalmanHasni, lol ok. You should have really put all these contraints on OP so people know exactly why and what your expected output is, I will update my answer later on.
|
1

Seems like there is a problem in your xpath towards the end. Try this piece of code:

driver.find_element_by_xpath("//div[contains(@class,'j-C') and contains(@class ,'j-C-yj')]/div[1]")

3 Comments

Please provide more of the html code snippet. I am suspecting there are multiple div elements with class "j-C j-C-yj", hence it is not able to detect the concerned element
i have a another div with class='j-C j-C-yj tk-C' does it effect the output ?
It definitely will, if this is the parent div and it has multiple elements with class='j-C j-C-yj'. In that case, your code won't be able to click on the specified element. That's why I have asked for a more complete HTML code snippet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.