如何在python中使用硒和beautifulsoup单击元素?我得到了这些代码行,发现很难实现。我想单击每个迭代中的每个元素。没有分页或下一页。只有大约10个元素,单击最后一个元素后,它应该停止。有谁知道我该怎么办。这是我的代码
Background Events
这是站点。我想在每个平面图部分中抓取详细信息和图像,但是这很困难,因为仅在单击特定元素后才会显示图像。除了图像本身,我只能得到详细信息。自己尝试一下,以便您了解我的意思。
编辑: 我尝试过这种方法
slotEventOverlap:false,
问题是单击速度太快,导致图像无法加载。而且我在这里使用硒。是否有类似选择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()
这样的beautifulsoup的方法?
答案 0 :(得分:1)
您不能通过使用beautifulSoup与网站小部件进行交互,而需要使用 Selenium。。有两种方法可以解决此问题。
首先是获取10个元素的主包装(类),然后迭代到主类的每个子元素。
您可以通过xpath获取元素,并在每次迭代中将xpath中的最后一个数字递增1,以移至下一个元素。
答案 1 :(得分:0)
我打印一些结果以检查您的代码。
“详细信息”只有一项。 而“图片”不是要素。 (因此不可点击。)
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)
对于已编辑的版本,可以在单击()之前检查图像是否可见。
使用visible_of_element_located进行操作。