driver.find_elements_by_xpath
时间: 2023-04-19 14:02:27 浏览: 80
driver.find_elements_by_xpath是一个Selenium库中的函数,它可以通过XPath表达式来查找网页中的元素,并返回一个元素列表。这个函数可以用于自动化测试、爬虫等场景中,方便地定位和操作网页元素。
相关问题
优化一下下面的代码让name和adress_name并排显示import time from selenium import webdriver driver = webdriver.Chrome() driver.implicitly_wait(10) driver.get("https://www.shanghairanking.cn/institution") name = driver.find_elements_by_xpath('//*[@id="__layout"]/div/div[2]/div[2]/div[1]/div/div/div[2]/div[1]/span') address = driver.find_elements_by_xpath('//*[@id="__layout"]/div/div[2]/div[2]/div[1]/div/div/div[2]/div[3]/span[1]') for elements in name: print(elements.text) for address_name in address: print(address_name.text)
可以使用zip函数来将两个列表并排显示:
```python
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("https://www.shanghairanking.cn/institution")
names = driver.find_elements_by_xpath('//*[@id="__layout"]/div/div[2]/div[2]/div[1]/div/div/div[2]/div[1]/span')
addresses = driver.find_elements_by_xpath('//*[@id="__layout"]/div/div[2]/div[2]/div[1]/div/div/div[2]/div[3]/span[1]')
for name, address in zip(names, addresses):
print(name.text, address.text)
driver.quit()
```
这样就可以将name和address并排显示了。
把下面这个代码修改成获取全部大学名称import time from selenium import webdriver driver = webdriver.Chrome() driver.implicitly_wait(10) driver.get("https://www.shanghairanking.cn/institution") element = driver.find_elements_by_xpath('//*[@id="__layout"]/div/div[2]/div[2]/div[1]/div/div/div[2]/div[1]/span')[10] print(element.text)
可以尝试如下修改代码以获取全部大学名称:
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("https://www.shanghairanking.cn/institution")
# 点击“加载更多”按钮直到所有大学名称都被加载出来
while True:
try:
load_more_button = driver.find_element_by_xpath('//*[@id="__layout"]/div/div[2]/div[2]/div[1]/div/div/div[3]/button')
load_more_button.click()
time.sleep(1) # 等待页面加载
except:
break
# 获取所有大学名称
elements = driver.find_elements_by_xpath('//*[@id="__layout"]/div/div[2]/div[2]/div[1]/div/div/div[2]/div[2]/div/div')
for element in elements:
print(element.text)
driver.quit()
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)