How to get a specific text out from span class in Python (selenium)

时间:2017-05-16 09:25:20

标签: python selenium

I have the following span class:

<span class="k-pager-info k-label">1 - 25 of 93995 items</span>

and its Xpath is //*[@id="registerGrid"]/div[3]/span

I would like to get the number 93995 out.

I have tried the following:

driver.findElement(By.xpath("""//*[@id="registerGrid"]/div[3]/span""")).getText()

Traceback (most recent call last):

  File "<pyshell#47>", line 1, in <module>
    driver.findElement(By.xpath("""//*[@id="registerGrid"]/div[3]/span""")).getText()
AttributeError: 'WebDriver' object has no attribute 'findElement'

What shall I do then?

2 个答案:

答案 0 :(得分:3)

Syntax error: Use driver.find_element instead of driver.findElement

from selenium.webdriver.common.by import By
element = driver.find_element(By.XPATH, '//*[@id="registerGrid"]/div[3]/span')
text = element.text

Refer selenium docs for locating the elements

答案 1 :(得分:2)

function buildImageSection(data, result) {

    var index = 0;

    data.events.forEach(function(obj) {
      if(result[index]){
          obj.background = {"opacity": "50", "url": result[index].thumbImg };
      }else{
          //do we need to set a background property if no image?
      }

      index++;

      console.log(data);
      // console.log(data.events);
    }); // end forEach
}

你犯了一个错误,它将是find_element,而文本只是getText()

from selenium.webdriver.common.by import By
driver.find_element(By.XPATH, '//*[@id="registerGrid"]/div[3]/span')