[英]Allegro Selenium find and click button
堆栈:谷歌colab,python,selenium,BeautifulSoup
我想获取卖家的详细联系方式。点击“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 个不同卖家的 Xpath 的 3 个示例:
/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)
首次加载 web 站点时会弹出 window。 你需要关闭这个弹出窗口 window 然后它的工作。
wd = webdriver.Chrome()
wd.get('https://allegro.pl/oferta/zageszczarka-6-5km-90kg-higher-briggs-gratisy-9003885105#aboutSeller')
wd.maximize_window()
wd.find_element_by_xpath("/html/body/div[2]/div[5]/div/div[2]/div[2]/button[1]").click()
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[1]/ul/li[2]/div/button')
click_on_button.click()
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()
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[2]/div/button')
click_on_button.click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.