5

I need the sibling of a previously selected element(s).

For example:

<div class="dt">Technology</div>
<div class="da">GSM / CDMA / HSPA / EVDO / LTE</div>
<div class="dt">Dimensions</div>
<div class="da">149.9 x 70.4 x 7.8 mm (5.90 x 2.77 x 0.31 in)</div>
<div class="dt">Weight</div>
<div class="da">157 g (5.54 oz)</div>
<div class="dt">Build</div>
<div class="da">Back glass (Gorilla Glass 5), aluminum frame    </div>
<div class="dt">Type</div>
<div class="da">Dynamic AMOLED capacitive touchscreen, 16M colors</div>
<div class="dt">SIM</div>
<div class="da">Single SIM (Nano-SIM) or Hybrid Dual SIM)</div>

Let say I need the "Dimensions" and Type.

code:

dts = browser.find_elements_by_class_name("dt");
for dt in dts :

    if dt.("innerText") == "Dimensions":
        print(dt.("innerText") + "-" + dt.**FollowingSibling**())
    if dt.("innerText") == "Type":
        print(dt.("innerText") + "-" + dt.**FollowingSibling**())

expected output:

Dimensions - 149.9 x 70.4 x 7.8 mm (5.90 x 2.77 x 0.31 in)
Type - Dynamic AMOLED capacitive touchscreen, 16M colors

3 Answers 3

6

You can use text attribute or innerText as your wish.Try below code.

for dt in dts :

    if dt.text == "Dimensions":
        print(dt.text + "-" + dt.find_element_by_xpath("./following-sibling::div").text)
    if dt.text == "Type":
        print(dt.text + "-" + dt.find_element_by_xpath("./following-sibling::div").text)

OR

dts = browser.find_elements_by_class_name("dt");
for dt in dts :

    if dt.get_attribute("innerText")== "Dimensions":
        print(dt.get_attribute("innerText") + "-" + dt.find_element_by_xpath("./following-sibling::div").text)
    if dt.get_attribute("innerText") == "Type":
        print(dt.get_attribute("innerText") + "-" + dt.find_element_by_xpath("./following-sibling::div").text)

Output :

Dimensions-149.9 x 70.4 x 7.8 mm (5.90 x 2.77 x 0.31 in)
Type-Dynamic AMOLED capacitive touchscreen, 16M colors
Sign up to request clarification or add additional context in comments.

Comments

1

Try with the following code (finding elements by xpath):

dts = browser.find_elements_by_class_name("dt");
for dt in dts :

    if dt.("innerText") == "Dimensions":
        print(dt.("innerText") + "-" + dt.xpath("//div[contains(.,'Dimensions')]/following-sibling::div").text)
    if dt.("innerText") == "Type":
        print(dt.("innerText") + "-" + dt.xpath("//div[contains(.,'Type')]/following-sibling::div").text)

Comments

0

For finding the following sibling of an element you can use the CSS selector or XPath. Using XPath you can find the sibling element without knowing/matching the locator of the previous element. But if you use CSS selectors, then you have to use the selector for the sibling element of the element.

Xpath

dts = browser.find_elements_by_class_name("dt");
for dt in dts :

    if dt.text == "Dimensions":
        print(dt.text + "-" + dt.find_element_by_xpath("./following-sibling::div").text)
    if dt.text == "Type":
        print(dt.text + "-" + dt.find_element_by_xpath("./following-sibling::div").text)

CSS Selector

dts = browser.find_elements_by_class_name("dt")
das = browser.find_elements_by_css_selector(".dt + div")
for i in range(0, len(dts) :
    if dt.index(i).text == "Dimensions":
        print(dt.indext(i).text + "-" + da.index(i).text)
    if dt.index(i).text == "Type":
        print(dt.index(i).text + "-" + da.index(i)..text)

CSS selector will only find the divs that followed a tag with dt class. It will work on your given scenario because all the divs with dt class followed by a div with da tag that contains the value.

Comments

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.