Python 使用selenium提取类中的第一个元素
这个 给出了结果,并且存在多个共点。我只需要捕获类中的第一个元素 代码:Python 使用selenium提取类中的第一个元素,python,python-3.x,selenium,selenium-webdriver,selenium-chromedriver,Python,Python 3.x,Selenium,Selenium Webdriver,Selenium Chromedriver,这个 给出了结果,并且存在多个共点。我只需要捕获类中的第一个元素 代码: from selenium import webdriver from selenium.webdriver.common.keys import Keys from bs4 import BeautifulSoup import re import pandas as pd import os import html5lib import json import time from selenium import web
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import re
import pandas as pd
import os
import html5lib
import json
import time
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
from selenium.webdriver.common.keys import Keys
url = "https://www.google.com/"
chromedriver = r"C:\Users\me\chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.implicitly_wait(30)
driver.get(url)
search = driver.find_element_by_name('q')
search.send_keys("newyork pincode")
search.send_keys(Keys.RETURN)
time.sleep(5)
driver.quit()
找到名为
h998We mlo-c
的类并获取第一个元素将获得第一个pin码
find_element_by_class_name('h998We mlo-c')
将返回您之前询问的德里等城市的所有pin码,然后获取div
中的文本以获取Pincode
您可以使用
css选择器
或xpath
来实现
- Css选择器*推荐
div.IAznY div.title
- Xpath
//div[@class="IAznY"]//div[@class="title"]
WebDriverWait
而不是time.sleep(..)
以提高效率
首先,您需要以下导入:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
注意:按查找元素*
始终返回第一个元素,即使有许多元素具有相同的定位器
因此,您可以使用以下代码:
search = driver.find_element_by_name('q')
search.send_keys("newyork pincode")
search.send_keys(Keys.RETURN)
element = WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, 'div.IAznY div.title')))
print(element.text)
但是如果您需要像您所说的那样处理多个,并且不需要第一个元素,那么您可以使用。通过
查找元素。下面的代码是获取第二个元素的示例:
elements = WebDriverWait(driver, 10).until(expected_conditions.visibility_of_all_elements_located((By.CSS_SELECTOR, 'div.IAznY div.title')))
print(elements[1].text)
这是索引
[1]
,您可以在WebDriverWait
实现中看到第一个代码和第二个代码之间的差异,即位于的元素的可见性和位于的所有元素的可见性,并遵循Css选择器来识别元素
elements=WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, 'div.mlo-c div.title')))
print(elements[0].text)
打印在控制台上:
10001
您需要导入以下内容以执行上述代码
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
找到名为
h998We mlo-c
的类并获取第一个元素将为您提供第一个pin码。其他类型的类名称是否会更改pincodes@thoris回答更新了一点解释,希望能帮助更多。消息:没有这样的元素:无法定位元素:{“方法”:“css选择器”,“选择器”:“.h998We mlo-c”}find\u element\u by\u class\u name()
需要一个单独的类名,您需要发送两个。这将导致一个错误。明白了,让我重写我的解决方案。如果您只需要第一个元素,没有理由使用。查找元素\*()
,只需使用。查找元素\*()
。此外,上面的WebDriverWait()
语句返回等待的元素。通过等待,然后再进行另一次查找元素,您将在页面上点击两次。我编辑了你的答案,向你展示了简化的方法。我认为(并且已经证明)CSS选择器比XPath更快。鉴于此定位器不需要XPath,您可以轻松地将其转换为div.IAznY div.title
,它也更简短、可读性更强(我认为)。如果你愿意的话,我会让你把它编辑进去,因为我觉得它偏离了你想要的答案太多了,我无法去做。非常感谢,同意你所做的,对我来说非常清楚。您是selenium类中最好的stackoverflow用户之一。