在Python中使用Selenium进行分页表

时间:2017-06-29 19:46:44

标签: python selenium

我将通过以下链接查看表格:

http://cancer.sanger.ac.uk/cosmic/sample/overview?id=2120881

通过python中的selenium。这是代码:

driver = webdriver.Chrome()
driver.get('http://cancer.sanger.ac.uk/cosmic/sample/overview?id=2120881')
elem = driver.find_element_by_link_text("Variants")

while elem:
    elem.click()
    time.sleep(5)
    try:
        elem = driver.find_element_by_link_text("Next")
        print(elem.is_enabled())
        if 'disabled' in elem.get_attribute('class'):
            break
    except:
        print('Next not available or page not loaded!')
driver.quit()

我无法将显示的数量更改为100.我该怎么做?

另外,为什么即使按钮变得不可点亮,is_enabled()也会返回True?

提前致谢!

1 个答案:

答案 0 :(得分:3)

问:我无法将显示的数量更改为100.我该怎么做? 解决方案:您必须使用选择类(link)从下拉列表中选择值

问题:为什么即使按钮变得无法点击,is_enabled()也会返回True?

is_enabled()仅在正如您所看到的那样返回True时,True仅在启用时打印三次,并将其输入中断循环并按预期退出

刚刚使用Select和break print语句更新了代码:

npm install jspm -g

输出结果为:

from selenium import webdriver
import time
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://cancer.sanger.ac.uk/cosmic/sample/overview?id=2120881')
elem = driver.find_element_by_link_text("Variants")



while elem:
    elem.click()
    time.sleep(5)
    select = Select(driver.find_element_by_name('DataTables_Table_0_length'))
    select.select_by_value('100') # you can use any value 
    try:
        elem = driver.find_element_by_link_text("Next")
        print(elem.is_enabled())
        if 'disabled' in elem.get_attribute('class'):
            print "Before Break"
            break
    except:
        print('Next not available or page not loaded!')

driver.quit()