【问题标题】:Allegro Selenium find and click buttonAllegro Selenium 查找并单击按钮
【发布时间】: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


    【解决方案1】:

    第一次加载网站时会弹出一个窗口。你需要关闭这个弹出窗口然后它的工作。

    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()
    

    【讨论】:

    • hi @samsulislam 当您更改拍卖链接时,“pokaż”按钮的 Xpath 会更改。我还能如何找到并单击这些元素?以下解决方案不起作用: lick_on_button = wd.find_element(By.LINK_TEXT, 'pokaż') click_on_button.click() lick_on_button = wd.find_element(By.NAME, '_w7z6o _ypulx') click_on_button.click()
    • @dominik tu masz selektory '[data-box-name='SellerEmailShow']' [data-box-name='SellerEmailClick'] [data-analytics-click-value='sellerLogin'] samego pokaz sprzedawce nie musisz robic, wystarczy do adresu dodac #aboutSeller - 到 otworzy 十面板
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    相关资源
    最近更新 更多