我需要你的幫助,請。我正在使用 selenium 來點擊網頁上的一個按鈕,然后打開其他選項。不幸的是,它不能識別路徑元素。網頁模擬器是移動版的,因為它更容易訪問按鈕(桌面版的問題也是如此)。我試著點擊網頁上的其他按鈕,結果是正常的。
mobile_emulation = { "deviceName"/span>: "Galaxy S5" }
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(chromedriver_path, options=chrome_options)
driver.get('https://www...')
time.sleep(20)
Click1 = driver.find_element_by_xpath('//*[@id="header_3"]/div')
Click1.click()
錯誤是:
NoSuchElementException: no such element: 無法定位元素。{"method":"xpath","selector":"//*[@id="/span>header_3"]/div"}。
(會話資訊: chrome=93.0.4577.82)
網路代碼的影像如下:
謝謝你的幫助!
uj5u.com熱心網友回復:
嘗試一下
Click1 = driver.find_element_by_xpath('//div[@class="card-header"]'/span>)
或者
Click1 = driver.find_element_by_id("header_3")
uj5u.com熱心網友回復:
在Selenium中,有4種方法可以點擊。
我將使用這個xpath
。//div[contains(@id, 'header_3')]/div
代碼試用1 :
time.sleep(5)
driver.find_element_by_xpath("//div[contains(@id, 'header_3')]//div").click()
代碼試用2 :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[包含(@id, 'header_3')]/div")) .click()
代碼試用3 :
time.sleep(5)
button = driver.find_element_by_xpath("//div[contains(@id, 'header_3')]//div")
driver.execute_script("arguments[0].click();", button)
代碼試用4 :
time.sleep(5)
button = driver.find_element_by_xpath("//div[ contains(@id, 'header_3')]//div")
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(Google chrome)中檢查我們在HTML DOM中是否有unique條目。
檢查的步驟:
在Chrome中按F12 -> 進入element部分 -> 做一個CTRL F -> 然后粘貼xpath,看看你想要的element是否得到高亮。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/327089.html
標籤:

