使用硒读取多个文本元素时出现错误消息

时间:2019-12-04 14:44:16

标签: python pandas selenium selenium-webdriver

我正在单击某个链接,并希望读取给定类中的所有文本,并将其作为一行返回到pandas数据框中

这是我的代码

page_link = 'http://beta.compuboxdata.com/fighter'
wait = WebDriverWait(cdriver,10)
wait.until(EC.visibility_of_element_located((By.ID,'s2id_autogen1'))).send_keys('Deontay Wilder')
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'select2-result-label'))).click()
while True:
    wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'view_more'))).click()
    try:
        element = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'view_more')))
        element.click()
    except TimeoutException:
        break
fights['fighters'] = wait.until(EC.find_element_by_class_name((By.CLASS_NAME,'col-xs-4 col-sm-4 col-md-4 col-lg-4'))).text

这会返回错误消息:

TimeoutException: Message: 

我也尝试使用xpath,但仍然收到相同的错误消息:

fights['fighters'] = wait.until(EC.find_element_by_xpath((By.CLASS_NAME,'//div[@class="col-xs-4 col-sm-4 col-md-4 col-lg-4"]/div'))).text

我特别想获取以下数据: enter image description here

根据要求,这是完整的追溯

<ipython-input-94-e2b36e136c00> in <module>
     14 wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'select2-result-label'))).click()
     15 while True:
---> 16     wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'view_more'))).click()
     17     try:
     18         element = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'view_more')))

~\Anaconda3\lib\site-packages\selenium\webdriver\support\wait.py in until(self, method, message)
     78             if time.time() > end_time:
     79                 break
---> 80         raise TimeoutException(message, screen, stacktrace)
     81 
     82     def until_not(self, method, message=''):

TimeoutException: Message:

这是源HTML source

更新

我尝试添加一个for循环以读取多个文本元素,但仍然收到相同的错误

elements = wait.until(EC.find_element_by_class_name((By.CLASS_NAME,'col-xs-4 col-sm-4 col-md-4 col-lg-4')))
for e in elements:
    print(e.text)

1 个答案:

答案 0 :(得分:0)

这是一个解决方案。超时异常将在此处处理。

    age_link = 'http://beta.compuboxdata.com/fighter'
driver.get(age_link)
wait = WebDriverWait(driver,10)
wait.until(EC.visibility_of_element_located((By.ID,'s2id_autogen1'))).send_keys('Deontay Wilder')
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'select2-result-label'))).click()
#Click on View More until it exists
while True:
    try:
        element = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'view_more')))
        print("Clicking on View More")
        element.click()
    except TimeoutException:
        break
fighters = driver.find_elements_by_xpath("//div[@class='row row-bottom-margin-5']/div[2]")
#fighters = driver.find_elements_by_class_name("col-xs-4 col-sm-4 col-md-4 col-lg-4")
print(len(fighters))
# Print all the fighter names
for fighter in fighters:
    print(fighter.text)