Python 如何使用selenium和beautifulsoup单击元素?
如何在python中使用selenium和beautifulsoup单击元素?我得到了这些代码行,我发现很难实现。我想单击每个迭代中的每个元素。没有分页或下一页。只有大约10个元素,单击最后一个元素后,它应该停止。有人知道我该怎么做吗。这是我的密码Python 如何使用selenium和beautifulsoup单击元素?,python,selenium,selenium-webdriver,web-scraping,beautifulsoup,Python,Selenium,Selenium Webdriver,Web Scraping,Beautifulsoup,如何在python中使用selenium和beautifulsoup单击元素?我得到了这些代码行,我发现很难实现。我想单击每个迭代中的每个元素。没有分页或下一页。只有大约10个元素,单击最后一个元素后,它应该停止。有人知道我该怎么做吗。这是我的密码 from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebD
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import urllib
import urllib.request
from bs4 import BeautifulSoup
chrome_path = r"C:\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
url = 'https://www.99.co/singapore/condos-apartments/a-treasure-trove'
driver.get(url)
html = driver.page_source
soup = BeautifulSoup(html,'lxml')
details = soup.select('.FloorPlans__container__rwH_w') //Whole container of the result
for d in details:
picture = d.find('span',{'class':'Tappable-inactive'}).click() //the single element.
print(d)
driver.close()
这里是网站。我想在每个楼层平面图部分中刮取细节和图像,但这很困难,因为图像只有在单击特定元素后才会显示。除了图像本身,我只能得到细节。你自己试试,这样你就明白我的意思了
编辑:
我试过这个方法
for d in driver.find_elements_by_xpath('//*[@id="floorPlans"]/div/div/div/div/span'):
d.click()
问题是它的点击速度太快,图像无法加载。我也在这里使用硒。是否有任何方法可以像选择这样的美丽组格式
picture=d.find('span',{'class':'Tappable-inactive'))单击()
您无法使用beautifulSoup与网站小部件交互。您需要使用selenium。有两种方法来处理此问题
- 首先是获取10个元素的主包装器(类),然后迭代到主类的每个子元素
- 您可以通过xpath获取元素,并在每次迭代中将xpath中的最后一个数字增加1,以移动到下一个元素
- 首先是获取10个元素的主包装器(类),然后迭代到主类的每个子元素
- 您可以通过xpath获取元素,并在每次迭代中将xpath中的最后一个数字增加1,以移动到下一个元素
参考资料:我打印一些结果以检查您的代码 “详细信息”只有一项。 “图片”不是元素。(所以它是不可点击的。) 输出: 对于已编辑的版本,可以在单击()之前检查图像是否可见 使用要执行的元素的可见性
参考资料:我确实使用了selenium sirI,但我不知道如何混合,你不需要混合。当我们需要通过代码(包括单击、滚动等)与网页交互时,我们使用selenium。Beautiful soup不支持点击之类的活动。我确实使用了selenium sirI,但我不知道如何混合它们。你不需要混合它们。当我们需要通过代码(包括单击、滚动等)与网页交互时,我们使用selenium。Beautiful soup不支持单击等事件。div sir中有图像属性。请访问我提供的站点。在div中有一个图像属性。请访问我提供的网站
details = soup.select('.FloorPlans__container__rwH_w')
print(details)
print(len(details))
for d in details:
print(d)
picture = d.find('span',{'class':'Tappable-inactive'})
print(picture)