【发布时间】:2021-04-10 17:46:08
【问题描述】:
堆栈: 谷歌合作, Python, 硒, 美丽汤
我想获取卖家的详细联系信息。点击“pokaż”按钮后,即可看到数据。 如何找到并点击此按钮?
<div><div class="_31b2a_2D760"><span class="_kz8jr _31b2a_5mXDa _31b2a_1L727"></span><ul class="_17qy1 _1rj80 _1sql3 _3a4zn"><li><a data-box-name="AskSellerClick" class="_w7z6o" href="#zadaj-pytanie" rel="nofollow">pytanie do sprzedającego</a></li><li><div>dra****@************<!-- --> (<button data-box-name="SellerEmailShow" class="_w7z6o _ypulx" type="button">pokaż</button>)</div></li></ul></div><div class="_31b2a_2D760"><span class="_kz8jr _31b2a_5mXDa _31b2a_14F6t"></span><ul class="_17qy1 _1rj80 _1sql3 _3a4zn"><li><div>+48 ** *** ** **<!-- --> (<button data-box-name="SellerMobileShow" class="_w7z6o _ypulx" type="button">pokaż</button>)</div></li><li><div>+48 *** *** ***<!-- --> (<button data-box-name="SellerMobileShow" class="_w7z6o _ypulx" type="button">pokaż</button>)</div></li></ul></div></div>
我的代码:
!apt update
!apt install chromium-chromedriver
!pip install selenium
from bs4 import BeautifulSoup
from selenium.webdriver import ActionChains
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome(options=options)
wd.get('https://allegro.pl/oferta/zageszczarka-6-5km-90kg-higher-briggs-gratisy-9003885105#aboutSeller')
click_on_button = wd.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div[10]/div/div/div/div/div[7]/div[2]/div/div/div[2]/div/div[2]/section/div/div/div/div[2]/div/div/div[2]/section[2]/div/div/div[2]/ul/li[1]/div/button')
click_on_button.click()
soup = BeautifulSoup(wd.page_source, 'html.parser')
dd = soup.find_all('ul', attrs={'class':'_17qy1 _1rj80 _1sql3 _3a4zn'})
print(dd)
消息:
ElementClickInterceptedException: Message: element click intercepted: Element <button data-box-name="SellerMobileShow" class="_w7z6o _ypulx" type="button">...</button> is not clickable at point (597, 537). Other element would receive the click: <div class="_9f0v0 _jkrtd mpof_ki_s" slot="button">...</div>
(Session info: headless chrome=87.0.4280.66)
我还能如何找到这些按钮?不幸的是,当卖方发生变化时,Xpath 也会发生变化。以下是来自 3 个不同卖家的 3 个 Xpath 示例:
/html/body/div[2]/div[3]/div/div/div[10]/div/div/div/div/div[6]/div[2]/div/div/div[2]/div/div[2]/section/div/div/div/div[2]/div/div/div[2]/section[2]/div/div/div[1]/ul/li[2]/div/button
/html/body/div[2]/div[3]/div/div/div[10]/div/div/div/div/div[4]/div[2]/div/div/div[2]/div/div[2]/section/div/div/div/div[2]/div/div/div[2]/section[2]/div/div/div[1]/ul/li[2]/div/button
/html/body/div[2]/div[3]/div/div/div[10]/div/div/div/div/div[7]/div[2]/div/div/div[2]/div/div[2]/section/div/div/div/div[2]/div/div/div[2]/section[2]/div/div/div[1]/ul/li[2]/div/button
消息:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[2]/div[3]/div/div/div[10]/div/div/div/div/div[7]/div[2]/div/div/div[2]/div/div[2]/section/div/div/div/div[2]/div/div/div[2]/section[2]/div/div/div[1]/ul/li[2]/div/button"}
(Session info: headless chrome=87.0.4280.66)
【问题讨论】:
标签: python selenium web-scraping beautifulsoup google-colaboratory