知网上有一个每页显示20条和50条的选项,我想通过selenium用python实现这个按键的点击,给出参考代码
时间: 2024-05-07 13:22:03 浏览: 7
可以利用Selenium的`find_element_by_xpath`方法来定位对应的元素,然后使用`click()`方法模拟点击操作。下面是一个示例代码:
```python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 创建浏览器实例
driver = webdriver.Chrome()
# 打开网页
driver.get('http://www.cnki.net/')
# 等待页面加载完成
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.ID, 'txt_1_sel')))
# 定位每页显示20条和50条的选项
twenty_per_page = driver.find_element_by_xpath('//*[@id="PageList"]/a[2]')
fifty_per_page = driver.find_element_by_xpath('//*[@id="PageList"]/a[3]')
# 模拟点击操作
twenty_per_page.click()
fifty_per_page.click()
# 关闭浏览器
driver.quit()
```
在上面的代码中,我们先打开了知网的首页,然后等待页面加载完成。接着,通过`find_element_by_xpath`方法定位了每页显示20条和50条的选项,并分别模拟了点击操作。最后关闭了浏览器。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)