英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论