Selenium Webdriver finding an element in a sub-element

前端 未结 4 1209
臣服心动
臣服心动 2021-01-30 01:38

I am trying to search for an element in a sub-element with Selenium (Version 2.28.0), but selenium des not seem to limit its search to the sub-element. Am I doing this wrong or

相关标签:
4条回答
  • 2021-01-30 01:51

    Use the following:

    element2 = driver.find_element_by_cssselector("css=div[title='div2']")
    element2.find_element_by_cssselector("p[@class='test']").text 
    

    Please let me know if you have any problems.

    0 讨论(0)
  • 2021-01-30 02:02

    If you start an XPath expression with //, it begins searching from the root of document. To search relative to a particular element, you should prepend the expression with . instead:

    element2 = driver.find_element_by_xpath("//div[@title='div2']")
    element2.find_element_by_xpath(".//p[@class='test']").text
    
    0 讨论(0)
  • 2021-01-30 02:07

    Chrome Webdriver :

    element = driver.find_element_by_id("ParentElement")
    localElement = element.find_element_by_id("ChildElement")
    print(localElement.text)
    
    0 讨论(0)
  • 2021-01-30 02:12

    This is how you search for element or tag in CSS subclass and I believe that it works for multilevel situation as well:

    Sample HTML:

    <li class="meta-item">
     <span class="label">Posted:</span>
     <time class="value" datetime="2019-03-22T09:46:24+01:00" pubdate="pubdate">22.03.2019 u 09:46</time>
    </li>

    This is how you would get pubdate tag value for example.

    published = driver.find_element_by_css_selector('li>time').get_attribute('datetime')
    
    0 讨论(0)
提交回复
热议问题