【问题标题】:Unable to find `class` on a Google page using selenium and python无法使用 selenium 和 python 在 Google 页面上找到“类”
【发布时间】:2019-07-16 01:11:24
【问题描述】:

我想在 Google 网页上按下一个按钮,但 selenium 找不到它。

页面显示如下:

这是html:

https://search.google.com/search-console/about

<span class="RveJvd snByac">Start now</span>

代码如下:

def show_webpage(judge_url):
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.get(SITE)
    button_element = driver.find_element_by_class_name('RveJvd snByac')
    button_element[1].click()

    html_source = driver.page_source

    driver.close()
    return html_source

这是错误:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such 
element: Unable to locate element: {"method":"css 
selector","selector":".RveJvd snByac"}

【问题讨论】:

  • RveJvdsnByac 是两个类。空格是class 属性中的分隔符。

标签: python selenium selenium-webdriver xpath webdriverwait


【解决方案1】:

正如 Micheal 所说,find_element_by_class_name 一次只接受一个类名作为参数。你正在通过两个。如果你想使用两个类名,那么你可以使用 css 选择器,如下所示。

def show_webpage(judge_url):
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.get(SITE)
    button_element = driver.find_element_by_css_selector('.RveJvd.snByac')
    button_element[1].click()

    html_source = driver.page_source

    driver.close()
    return html_source

【讨论】:

    【解决方案2】:

    find_element_by_class_name() 中传递多个classNames 将导致Invalid selector: Compound class names not permitted using find_element_by_class_name

    此外,类名 例如RveJvdsnByac 等看起来充满活力。

    但是,要在 Google 网页 https://search.google.com/search-console/about 上单击文本为 立即开始 的按钮,您可以使用以下 Locator Strategy

    • 代码块:

      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
      chrome_options = webdriver.ChromeOptions() 
      chrome_options.add_argument("start-maximized")
      # chrome_options.add_argument('disable-infobars')
      driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
      driver.get("https://search.google.com/search-console/about")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Improve your performance on Google Search']//following::div[1]//span[text()='Start now']"))).click()
      

    【讨论】:

      【解决方案3】:
      1. 这些类名很可能在每次加载页面时都会发生变化,您应该坚持使用 span 标记的 Start now 文本
      2. 无法保证该元素会立即在DOM 中可用,因此请考虑使用Explicit Wait 以确保该文档存在

      建议的代码更改:

      driver.get("https://search.google.com/search-console/about")
      
      start_now = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Start now']")))
      driver.execute_script("arguments[0].click()", start_now)
      

      更多信息:How to use Selenium to test web applications using AJAX technology

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-03-19
        • 2020-12-04
        • 2011-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-05
        相关资源
        最近更新 更多