Selenium giving error: "selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator"

huangapple go评论63阅读模式
英文:

Selenium giving error: "selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator"

问题

I attempted to retrieve an tag using its classname from a webpage using Selenium.

我尝试使用Selenium从网页中使用其类名检索标记。

I wrote the following:

我写了以下内容:

import os
from selenium import webdriver

def launchBrowser():
    os.environ['PATH'] += r"/SeleniumDrivers/chromedriver_mac_arm64"
    driver = webdriver.Chrome()
    #driver.implicitly_wait(15)
    driver.get("https://www.geeksforgeeks.org/find_element_by_id-driver-method-selenium-python/")
    element = driver.find_element("class_name", "header-main_wrapper")
    #element = driver.find_element_by_xpath("//div[@class=bp3-button bp3-minimal]")
    #link = element.get_attribute("href")
    #link.click()
    while(True):
        pass

launchBrowser()

在调试后,element = driver.find_element("class_name", "header-main_wrapper") 返回了上述错误。我不确定为什么。我认为我按照方法签名的方式进行了操作,所以不确定我做错了什么。

英文:

I attempted to retrieve an tag using its classname from a webpage using Selenium.

I wrote the following:

import os
from selenium import webdriver

def launchBrowser():
    os.environ['PATH'] += r"/SeleniumDrivers/chromedriver_mac_arm64"
    driver = webdriver.Chrome()
    #driver.implicitly_wait(15)
    driver.get("https://www.geeksforgeeks.org/find_element_by_id-driver-method-selenium-python/")
    element = driver.find_element("class_name", "header-main_wrapper")
    #element = driver.find_element_by_xpath("//div[@class=bp3-button bp3-minimal]")
    #link = element.get_attribute("href")
    #link.click()
    while(True):
        pass

launchBrowser()

After debugging element = driver.find_element("class_name", "header-main_wrapper") returns the error above. I'm unsure why. I think I followed the method signature properly which is why I'm unsure what I'm doing wrong.

答案1

得分: 1

将以下内容翻译为中文:

Change the below line:

element = driver.find_element("class_name", "header-main_wrapper")

To:

element = driver.find_element(By.CLASS_NAME, "header-main_wrapper")

Imports required:

from selenium.webdriver.common.by import By
英文:

Change the below line:

element = driver.find_element("class_name", "header-main_wrapper")

To:

element = driver.find_element(By.CLASS_NAME, "header-main_wrapper")

Imports required:

from selenium.webdriver.common.by import By

huangapple
  • 本文由 发表于 2023年5月25日 23:25:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定