【问题标题】:Selenium click button with weird text (windows, chromedriver)带有奇怪文本的 Selenium 单击按钮(windows,chromedriver)
【发布时间】:2021-01-25 07:48:36
【问题描述】:

我试图让我的机器人点击购买按钮,但它有一些奇怪的文字,无法让我的代码正常工作(我认为这就是为什么如果我错了不要生气)。

HTML

我尝试过的代码:

driver.find_element_by_xpath('//button[@text()="20 Bits"]').click()

我也尝试过上课,但似乎根本不起作用。

【问题讨论】:

    标签: python windows selenium selenium-chromedriver


    【解决方案1】:

    去掉text()前面的@。文字周围有很多空白,所以你想使用contains

    driver.find_element_by_xpath('//button[contains(text(),"20 Bits")]').click()
    

    我建议使用

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    .....
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(),"20 Bits")]'))).click()
    

    【讨论】:

      【解决方案2】:

      试试这个:

      driver.find_element_by_xpath(“//button[@class=‘purchase bits flat no-cap’]”).click()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-27
        • 1970-01-01
        • 2015-10-18
        • 2020-04-14
        • 2022-01-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多