我还试图从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:UsersselkoPycharmProjectsshoschvenvlibsite-packagesdiscordextcommandscore.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:UsersselkoPycharmProjectsshoschaternoscommand.py", line 38, in google
firstemgo = driver.find_element_by_css_selector(".eKjLze > div:nth-child(1) > div:nth-child(1) > "
File "C:UsersselkoPycharmProjectsshoschvenvlibsite-packagesseleniumwebdriverremotewebdriver.py", line 598, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "C:UsersselkoPycharmProjectsshoschvenvlibsite-packagesseleniumwebdriverremotewebdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:UsersselkoPycharmProjectsshoschvenvlibsite-packagesseleniumwebdriverremotewebdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:UsersselkoPycharmProjectsshoschvenvlibsite-packagesseleniumwebdriverremoteerrorhandler.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:UsersselkoPycharmProjectsshoschvenvlibsite-packagesdiscordextcommandsbot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:UsersselkoPycharmProjectsshoschvenvlibsite-packagesdiscordextcommandscore.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:UsersselkoPycharmProjectsshoschvenvlibsite-packagesdiscordextcommandscore.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)
永远不要使用自动生成的定位器
请尝试这个:
@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()
您提供的定位器是错误的。你可以试试下面的代码
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"]')