我想按一下Google网页上的按钮,但是硒找不到它。
这是html:
https://search.google.com/search-console/about
<span class="RveJvd snByac">Start now</span>
代码如下:
def show_webpage(judge_url):
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(SITE)
button_element = driver.find_element_by_class_name('RveJvd snByac')
button_element[1].click()
html_source = driver.page_source
driver.close()
return html_source
这是错误:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such
element: Unable to locate element: {"method":"css
selector","selector":".RveJvd snByac"}
答案 0 :(得分:1)
正如Micheal所说,find_element_by_class_name一次仅接受一个类名作为参数。您正在通过两个。如果要使用两个类名,则可以使用css选择器,如下所示。
def show_webpage(judge_url):
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(SITE)
button_element = driver.find_element_by_css_selector('.RveJvd.snByac')
button_element[1].click()
html_source = driver.page_source
driver.close()
return html_source
答案 1 :(得分:1)
在find_element_by_class_name()
中传递多个 classNames 将得到Invalid selector: Compound class names not permitted using find_element_by_class_name
此外,类名例如 RveJvd , snByac 等看起来很动态。
但是,要在Google网页https://search.google.com/search-console/about
上单击文本为立即开始的按钮,您可以使用以下Locator Strategy:
代码块:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("start-maximized")
# chrome_options.add_argument('disable-infobars')
driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://search.google.com/search-console/about")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Improve your performance on Google Search']//following::div[1]//span[text()='Start now']"))).click()
答案 2 :(得分:1)
Start now
文本建议的代码更改:
driver.get("https://search.google.com/search-console/about")
start_now = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Start now']")))
driver.execute_script("arguments[0].click()", start_now)
更多信息:How to use Selenium to test web applications using AJAX technology