我花了很多時間在一個看似簡單的問題上,但我仍在尋找答案。
這是 print(browser.page_source) 的輸出,所以我的路徑是正確的。
這是我試圖用 Selenium 點擊但沒有任何運氣的按鈕。我沒有使用 Selenium 和 SVG 的經驗。我不確定 SVG 是否在這里發揮作用。我認為一個簡單的 xpath 與 .click(0 事件應該這樣做,但到目前為止還沒有骰子。

<button type="button" disabled="" class=“kl-button kl-button-pri ce-blue-button kl—button-disabled">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 22 22” fill="none" stroke=“bluecolor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class=“school_search green_fields-search”>
<circle cx="12” cy="12” r=“9”></circle>
<path d="M17 19l-4.25-4.15"></path>
</svg>
School Finder<span hljs-keyword">class="green_fields"></span></span>
</button>
這是我到目前為止所嘗試的。
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
browser = webdriver.Chrome(ChromeDriverManager().install())
# browser.get('https://google.com')
browser.get(url)
# find_all = browser.find_element_by_xpath("//div[@class='school_load']//div[2]//button[2]")
# find_all = browser.find_element_by_xpath('//*[@id="LoadResults"]/div[2]/div[2]/div[1]/div[2]/button[1]')
# browser.find_element_by_xpath('//button[@ and child::svg[@]]').click()
# find = browser.find_element_by_xpath('//button[@]')
find = browser.find_element_by_xpath('//button[@]//*[name()="svg"][@]')
print(find)
find.click()
# browser.find_element_by_xpath('//button/*[name()="svg"][@]').click()
我得到了輸出
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
或者
<selenium.webdriver.remote.webelement.WebElement (session="2f2ab3421ll4fd509ecbeb762bd11d8e", element="44d4ce11-5a26-412a-8md1-83c59plm7c12")>
無論我嘗試什么,我都無法點擊按鈕。任何幫助表示贊賞。
uj5u.com熱心網友回復:
您的問題由 2 個單獨的問題組成。
- 第一個問題是 NoSuchElementException。當頁面尚未加載但
find_element代碼已執行并因此找不到元素時,可能會發生此錯誤。我建議使用 WebDriverWait 在查找元素之前等待它被加載。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
xpath = '//button[...]'
try:
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH)))
driver.find_element(By.XPATH, xpath)
except TimeoutException:
print('Waiting for the element to appear on the webpage has expired. Wrong xpath?')
- 您似乎遇到的第二個問題是無法單擊該元素。發生這種情況時,您始終可以使用 ActionChains 將滑鼠移動到某個元素,然后單擊該位置。
from selenium.webdriver.common.action_chains import ActionChains
element = find_element(...)
ActionChains(driver).move_to_element(element).click().perform()
uj5u.com熱心網友回復:
根據您在問題中提出的代碼,您正在等待。
您正在嘗試在頁面加載之前訪問該元素。
請試試這個:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome(ChromeDriverManager().install())
browser.get(url)
wait = WebDriverWait(driver, 20)
find = wait.until(EC.visibility_of_element_located((By.XPATH, '//button[@]//*[name()="svg"][@]')))
print(find)
find.click()
uj5u.com熱心網友回復:
有一種特殊的方法來定位 svg 網頁元素,請使用下面的 xpath
//*[local-name()='svg' and @class='“school_search']/..
基本上有 4 種方法可以在 Selenium 中單擊。
代碼試用1:
time.sleep(5)
driver.find_element_by_xpath("//*[local-name()='svg' and @class='“school_search']/..").click()
代碼試用2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[local-name()='svg' and @class='“school_search']/.."))).click()
代碼試用3:
time.sleep(5)
button = driver.find_element_by_xpath("//*[local-name()='svg' and @class='“school_search']/..")
driver.execute_script("arguments[0].click();", button)
代碼試用4:
time.sleep(5)
button = driver.find_element_by_xpath("//*[local-name()='svg' and @class='“school_search']/..")
ActionChains(driver).move_to_element(button).click().perform()
進口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
PS:請檢查dev tools(谷歌瀏覽器)我們是否有唯一的條目HTML DOM。
檢查步驟:
Press F12 in Chrome- >去element節- >做一個CTRL F- >再貼上xpath看看,如果你需要的element是越來越強調與1/1匹配的節點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/312350.html
