【问题标题】:Helium or Selenium wont click button or Link氦气或硒不会单击按钮或链接
【发布时间】:2021-06-11 10:18:06
【问题描述】:

我正在抓取一个名为 skechers 的运动鞋网站,现在当向下滚动以加载更多产品时出现 LOAD MORE 链接/按钮时,它会单击元素但没有加载。

这是我的代码:

from helium import*
import time
from bs4 import BeautifulSoup


s = start_firefox("https://www.skechers.com/men/shoes/boots/?srule=price-low-to-high&start=0&sz=24")

time.sleep(5)

for x in range(1,5):
    scroll_down(num_pixels=1000)
    time.sleep(3)
    try:
        click(Button('LOAD MORE'))
        time.sleep(10)
        
    except:
        continue

soup = BeautifulSoup(s.page_source,"lxml")

kill_browser()

现在我尝试了 click("LOAD MORE") 和 click(Link("LOAD MORE")),但它们似乎都不起作用:(

【问题讨论】:

    标签: python python-3.x selenium beautifulsoup error-handling


    【解决方案1】:
    from helium import*
    import time
    from bs4 import BeautifulSoup
    
    
    s = start_firefox("https://www.skechers.com/men/shoes/boots/?srule=price-low-to-high&start=0&sz=24")
    
    time.sleep(5)
    
    for x in range(1,5):
        scroll_down(num_pixels=1000)
        time.sleep(3)
        try:
            button=s.find_element_by_xpath('//button[contains(text(),"Load More")]')
            button.click()
            time.sleep(10)
            
        except:
            continue
    
    soup = BeautifulSoup(s.page_source,"lxml")
    
    kill_browser()
    

    使用 xpath ,由于某种原因,该方法在 firefox 中不起作用,但在 chrome 中运行良好

    【讨论】:

    • 嗨,我厌倦了你上面的代码,但它仍然只是点击它,没有加载。我在 Firefox 和 chrome 中都试过了 :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 2016-03-15
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多