0

I'm trying to get a text from a class tag using selenium and I kinda hit a dead end. All the classes name are the same "legend-row" but I only need the 3rd one, the one that has "1.09TB" text.

I have tried to find the element by xpath and no luck the only way that I managed to get the value from those classes was by using the below line of code

driver.find_element_by_class_name("legend-row").text

which gets me all the texts, 134.46Tb, 8.96TB, 1.09TB and Threshold Level 90%. Is there any way that I can only query the 3rd one? See below the classes from the URL

<g class="legend" style="font-size: 14px;">
    <g class="legend-row" transform="translate(102,-32.400000000000006)">
        <rect y="-30.599999999999998" width="14" height="14" style="fill: rgb(125, 123, 140); stroke: rgb(125, 123, 140);"/>
        <text x="18" y="-18">Allocated:</text>
        <text x="109.421875" y="-18"> 134.46 TB</text>
    </g>
    <g class="legend-row" transform="translate(102,-10.800000000000004)">
        <rect y="-30.599999999999998" width="14" height="14" style="fill: rgb(110, 37, 133); stroke: rgb(110, 37, 133);"/>
        <text x="18" y="-18">Spare:</text>
        <text x="109.421875" y="-18"> 8.96 TB</text>
    </g>
    <g class="legend-row" transform="translate(102,10.799999999999997)">
        <rect y="-30.599999999999998" width="14" height="14" style="fill: rgb(130, 224, 170); stroke: rgb(130, 224, 170);"/>
        <text x="18" y="-18">Unallocated:</text>
        <text x="109.421875" y="-18"> 1.09 TB</text>
    </g>
    <g class="legend-th-row" transform="translate(102,32.39999999999999)">
        <text x="0" y="0" class="chart-status-icon" style="font-size: 14px; fill: rgb(242, 175, 0);"></text>
        <text x="18" y="0">Threshold Level: 90%</text>
    </g>
</g>

Thanks for the help,

6
  • 1
    Can you please point us to the URL so that we can investigate. Commented Jul 28, 2020 at 6:58
  • Hey @Pythonologist The url that I'm scrapping is not open to the public, it's an internal URL from a server in a local network. If I past here the source page would it help? Commented Jul 28, 2020 at 7:05
  • You are right about the index number [3] but for some reason the element can't be found. This is the xpath for the 3rd element "//*[@id="sc-storage-center-top-disk-folders_0"]/div/sc-storage-center-top-disk-folders/div/div[2]/dui-donut-chart/svg/g/g/g[3]" but if I try "driver.find_element_by_xpath("""//*[@id="sc-storage-center-top-disk-folders_0"]/div/sc-storage-center-top-disk-folders/div/div[2]/dui-donut-chart/svg/g/g/g[3]""").text" i get the message "Message: Unable to locate element" Commented Jul 28, 2020 at 7:19
  • This is the message I'm getting from your suggested answer. "Message: Unable to locate element: (//g[@class='legend']/g[@class='legend-row'])[3]" Commented Jul 28, 2020 at 7:20
  • @PeterFranca As you mentioned ..//dui-donut-chart/svg/g/g/g[3], can you update the question with the text based html please? Commented Jul 28, 2020 at 9:03

3 Answers 3

1

To print the text 1.09 TB you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using XPATH and text attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//dui-donut-chart/*[name()='svg']/*[name()='g']/*[name()='g']//*[name()='g']//text[., 'Unallocated:']//following::text[1]"))).text)
    
  • Using CSS_SELECTOR and get_attribute():

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "dui-donut-chart>svg>g>g g:nth-of-type(3) text:nth-of-type(2)"))).get_attribute("innerHTML"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant discussions in:

Sign up to request clarification or add additional context in comments.

1 Comment

Hey guys I managed to get the script working. I used all the mentioned options/answers here to come up with another idea which is working now. Thank you all for all the overwhelming help.
0

Try the below code I have written by referring to your attached image -

mytxt = driver.find_element_by_xpath("(//g[@class='legend']/g[@class='legend-row'])[3]").text
Split_text = mytxt.split()

print(Split_text[1] + ' ' + Split_text[2])

Output = 1.09 TB

enter image description here

enter image description here

Note - Please play with the index number i.e. in our case [3].

Comments

0

Peter, If you're getting all of three values as text/string, then the easiest way to do that will be

string = "134.46Tb, 8.96TB, 1.09TB"
third_item = string.split(',')[2]

In the third_item variable, you'll have 1.09TB

Another way you can directly use split() after your function like this -

driver.find_element_by_class_name("legend-row").text.split(',')[2]

split() will convert the text into a list of strings separated with comma. You we can get a particular item using slicing.

6 Comments

Hey @Indrajeet Singh Thanks for the suggestion. Using this line UNALLOCATED_SPACE = driver.find_element_by_class_name("legend").text.split(',')[2] I get IndexError: list index out of range. To use the split function Do I need to import it first?
Try using this. unallocatedSpace = driver.find_element_by_class_name("legend").text.split()[2]
I got it working with this line here UNALLOCATED_SPACE = driver.find_element_by_class_name("legend").text.split()[5] Result is 1.09. I'm wondering why the TB was left off? This is kinda important for me query any guess?
can you paste the exact text you're getting in UNALLOCATED_SPACE ? , so we can see it. Because i am am also getting TB from the string you mentioned in the question
Hey guys I managed to get the script working. I used all the mentioned options/answers here to come up with another idea which is working now. Thank you all for all the overwhelming help.
|

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.