【问题标题】:how to click button in headless and button have div tag inside it using selenium python?如何使用 selenium python 单击无头按钮和按钮中的 div 标签?
【发布时间】:2019-12-20 05:47:44
【问题描述】:

在没有无头方法的情况下使用此代码.. 网址链接:https://www.na-kd.com/en/sweaters?sortBy=popularity&count=108

try:
     element = self.driver.find_element_by_xpath('//*[@id="container"]/div/div/div[3]/div/div[4]/div/div[1]/div[2]/div[1]/button')
     self.driver.execute_script("arguments[0].click();", element)

except Exception as e:
     print('Error in clicking BTN : '+str(e))

因为这个 btn 里面有 div-tag,所以它不能与 headless 和 virtual-display 一起使用。

我也试试等待:

    try:
        element=WebDriverWait(self.driver, 20).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="container"]/div/div/div[3]/div/div[4]/div/div[1]/div[2]/div[1]/button')))
        self.driver.execute_script("arguments[0].click();", element)

    except Exception as e:
        print('Error in clicking BTN : '+str(e))

chromedriver --version
ChromeDriver 78.0.3904.70
谷歌浏览器 78.0.3904.108

【问题讨论】:

  • 我看不到任何按钮,你能把这个的html代码发给我们
  • <div class="qb qc qy qtl"><div class="qtm"><button class="qb6 qcf qb8 qcz qcy qd0 qcx qor qos qot qou qbh qbi qbj qcn qbk qbl qaq qat qaw qaz qco qbo qef qbm qtn qto qkm qtp qtq qtr qip qiq qir qis qts qc6 qh" type="button"><span class="qa8 qr qbk qd8 qgk qix qiy qiz qcd qce"><div class="qtt qtu qcq">Load more products</div></span></button></div><div class="qtv qtw qtx qty qan qex ql9 qtz qu0 qey qu1 qu2 qu3"></div><div class="qa6 qn9 qu4">108 of 1736 products</div></div>
  • 在页面最后的“加载更多产品”按钮...
  • 始终将代码、数据和错误消息放在有问题的地方,而不是在评论中。它将更具可读性,更多人会看到它。
  • 好吧,让我纠正这个..

标签: python selenium selenium-webdriver selenium-chromedriver headless


【解决方案1】:

在触发任何事件时使用无头模式 add window-size() 因为无头浏览器无法识别在没有窗口大小的情况下单击的位置。

点击Load more products按钮 Induce WebDriverWait() 并等待element_to_be_clickable() 并在下面使用xpath

要验证按钮是否被点击,只需向下滚动页面并从 div 标签中获取值。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time

# Headless option with window-size()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('window-size=1920x1080');

driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://www.na-kd.com/en/sweaters?sortBy=popularity&count=108&ssr=on&loadfailure=1")

# Load more products button  

element=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[.//div[text()='Load more products']]")))
driver.execute_script("arguments[0].click();", element)

# To verify that whether button is clicked or not
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2) #wait for page to load
print(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='qa6 qmz qn0']"))).text)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 2023-03-16
    • 2019-09-01
    • 2021-09-11
    • 1970-01-01
    相关资源
    最近更新 更多