【问题标题】:I am trying to make Selenium click on the first google result, but it always gives me an error我试图让 Selenium 点击第一个谷歌结果,但它总是给我一个错误
【发布时间】:2021-07-05 08:32:02
【问题描述】:

我也尝试从 XPath 中定位元素,但也没有成功。

代码:

@commands.command()
async def google(self, ctx, *, message):
    global cookieacgo
    global firstemgo
    global search_bar
    driver = webdriver.Chrome('C:/Users/selko/Downloads/chromedriver.exe')
    driver.get("https://google.com/xhtml")
    cookieacgo = driver.find_element_by_xpath("//*[@id='L2AGLb']/div").click()
    ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable)
    search_bar = driver.find_element_by_xpath("/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input")
    search_bar.send_keys(message)
    search_bar.send_keys(Keys.ENTER)
    firstemgo = driver.find_element_by_css_selector(".eKjLze > div:nth-child(1) > div:nth-child(1) > "
                                                    "div:nth-child(1) > div:nth-child(1) > a:nth-child(1) > "
                                                    "h3:nth-child(2)")
    ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable)
    firstemgo.click()

错误:

Traceback (most recent call last):
  File "C:\Users\selko\PycharmProjects\shosch\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\selko\PycharmProjects\shosch\aternoscommand.py", line 38, in google
    firstemgo = driver.find_element_by_css_selector(".eKjLze > div:nth-child(1) > div:nth-child(1) > "
  File "C:\Users\selko\PycharmProjects\shosch\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 598, in find_element_by_css_selector
    return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
  File "C:\Users\selko\PycharmProjects\shosch\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\selko\PycharmProjects\shosch\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\selko\PycharmProjects\shosch\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".eKjLze > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > a:nth-child(1) > h3:nth-child(2)"}
  (Session info: chrome=91.0.4472.124)


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\selko\PycharmProjects\shosch\venv\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\selko\PycharmProjects\shosch\venv\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\selko\PycharmProjects\shosch\venv\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".eKjLze > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > a:nth-child(1) > h3:nth-child(2)"}
  (Session info: chrome=91.0.4472.124)

【问题讨论】:

  • 你能详细解释一下你想做什么吗?
  • 我认为它找不到给定的css_selector 尝试解决这个问题

标签: python selenium xpath css-selectors discord.py


【解决方案1】:

切勿使用自动生成的定位器!
请试试这个:

@commands.command()
async def google(self, ctx, *, message):
    global cookieacgo
    global firstemgo
    global search_bar
    driver = webdriver.Chrome('C:/Users/selko/Downloads/chromedriver.exe')
    wait = WebDriverWait(driver, 20)
    driver.get("https://google.com/xhtml")
    wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='L2AGLb']/div"))).click()
    search_bar = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[name=q]")))
    search_bar.send_keys(message)
    search_bar.send_keys(Keys.ENTER)
    firstemgo = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='g']/h2/..//a[@href]")))
    firstemgo.click()

【讨论】:

    【解决方案2】:

    您提供的定位器有误。你可以试试下面的代码

    driver.get("https://google.com/xhtml")
    # cookieacgo = driver.find_element_by_xpath("//*[@id='L2AGLb']/div").click()
    ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable)
    search_bar = driver.find_element_by_xpath("/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input")
    search_bar.send_keys(message)
    search_bar.send_keys(Keys.ENTER)
    firstemgo = driver.find_element_by_css_selector("h3:nth-child(2)")
    ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable)
    firstemgo.click()
    

    而不是

    search_bar = driver.find_element_by_xpath("/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input")
    

    你可以使用

    search_bar = driver.find_element_by_css_selector('input[title="Search"]')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-02
      相关资源
      最近更新 更多