查找元素时遇到问题



我对编码很陌生,我正在尝试使用Selenium Chrome网络驱动程序在 Nike.com 上制作表单填充器。但是,出现了一个关于 cookied 的弹出窗口,我发现很难删除它,所以我可以填写表格。 这就是它的样子 我的代码看起来像这样:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
#Initialise a chrome browser and return it
def initialisebrowser():
browser=webdriver.Chrome(r'''C:Usersben_sDownloadschromedriver''')
return browser
#Pass in the browser and url, and go to the url with the browser
def geturl(browser, url):
browser.get(url)
#Initialise the browser (and store the returned browser)
browser = initialisebrowser()
#Go to a url(nike.com) with the browser
geturl(browser,"https://www.nike.com/gb/en_gb/s/register")
button = browser.find_element_by_class_name("nsg-button.nsg-grad--nike-orange.yes-button.cookie-settings-button.js-yes-button")
button.click()

当我运行此代码时,出现此错误:

Traceback (most recent call last):
File "C:Usersben_sDesktopNike Account Generator.py", line 19, in <module>
button = browser.find_element_by_class_name("nsg-button.nsg-grad--nike-orange.yes-button.cookie-settings-button.js-yes-button")
File "C:PythonPython36libsite-packagesseleniumwebdriverremotewebdriver.py", line 557, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:PythonPython36libsite-packagesseleniumwebdriverremotewebdriver.py", line 957, in find_element
'value': value})['value']
File "C:PythonPython36libsite-packagesseleniumwebdriverremotewebdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "C:PythonPython36libsite-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":"class name","selector":"nsg-button.nsg-grad--nike-orange.yes-button.cookie-settings-button.js-yes-button"}
(Session info: chrome=67.0.3396.87)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.17134 x86_64)

对问题的任何想法、指示或解决方案都非常适用

find_element_by_class_name接受一个类作为参数

browser.find_element_by_class_name('yes-button')

您提供的参数是所有 webelement 类的组合,由css_selector使用

browser.find_element_by_css_selector('.nsg-button.nsg-grad--nike-orange.yes-button.cookie-settings-button.js-yes-button')

请注意,您还需要在第一个类之前添加.,否则它将被视为标签名称。例如在这种情况下

browser.find_element_by_css_selector('button.nsg-button.nsg-grad--nike-orange.yes-button.cookie-settings-button.js-yes-button')

要确保按钮在单击之前存在,您可以使用显式等待

button = WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.CLASS_NAME, 'yes-button')))
button.click()

使用time.sleep等待弹出窗口加载,之后您可以使用cookie-settings-button-container

类,该类与btn有关,这将起作用。
time.sleep(1)                                                                                                                                                                   
button = browser.find_element_by_class_name("cookie-settings-button-container")
button.click()

相关内容

  • 没有找到相关文章

最新更新