Allegro Selenium 查找并单击按钮
Allegro Selenium find and click button
堆栈:
google合作社,
python,
硒,
BeautifulSoup
我想获取 seller.The 数据的详细联系信息,当您单击“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)
首次加载网站时会弹出 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()
堆栈: google合作社, python, 硒, BeautifulSoup
我想获取 seller.The 数据的详细联系信息,当您单击“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)
首次加载网站时会弹出 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()