Rynixx
Rynixx

Reputation: 15

Selenium getting text from WebElement Python

Hello I want the get the data printed from my size webelement

This is the site HTML

<div class="size-option"><label class="custom-input type-box size-box">
                            <input class="form-control" type="radio" name="sizeChartId" value="163" data-quantity="4">
                            <span class="checker-box">8</span>
                        </label><label class="custom-input type-box size-box">
                            <input class="form-control" type="radio" name="sizeChartId" value="164" data-quantity="5">
                            <span class="checker-box">8.5</span>
                        </label><label class="custom-input type-box size-box">
                            <input class="form-control" type="radio" name="sizeChartId" value="165" data-quantity="3">
                            <span class="checker-box">9</span>
                        </label><label class="custom-input type-box size-box">
                            <input class="form-control" type="radio" name="sizeChartId" value="167" data-quantity="8">
                            <span class="checker-box">10</span>
                        </label><label class="custom-input type-box size-box">
                            <input class="form-control" type="radio" name="sizeChartId" value="168" data-quantity="5">
                            <span class="checker-box">10.5</span>
                        </label><label class="custom-input type-box size-box">
                            <input class="form-control" type="radio" name="sizeChartId" value="169" data-quantity="3">
                            <span class="checker-box">11</span>
                        </label></div>

print(size.text) gives me

AttributeError: 'list' object has no attribute 'text'

Here is my element size=driver.find_elements_by_xpath("//*[@id='product']/div[1]/div/div[1]/div[2]/div/div[3]/div") and

for value in size:
    print(value.text)

Doesn't work

print(size[0].text)

Just give me empty, but I want the span Numbers in a list printed.

Upvotes: 0

Views: 43

Answers (1)

Prophet
Prophet

Reputation: 33353

Since "size" is a list of web elements if you wish to print text values of all it's elements, you have to iterate through the list and print each element text, like this:

size=driver.find_elements_by_xpath("//*[@id='product']/div[1]/div/div[1]/div[2]/div/div[3]/div")
for el in size:
    print(el.text)

Otherwise, if size intended to be a single element, I have no idea what is the "//*[@id='product']/div[1]/div/div[1]/div[2]/div/div[3]/div" XPath locating - if so you should use

size=driver.find_element_by_xpath("//*[@id='product']/div[1]/div/div[1]/div[2]/div/div[3]/div")

instead of

size=driver.find_elements_by_xpath("//*[@id='product']/div[1]/div/div[1]/div[2]/div/div[3]/div")

Upvotes: 1

Related Questions