如何在 Python 中使用 Selenium 解決 NoSuchElementException 錯誤消息?我正在嘗試從下拉菜單中選擇報告類型,并在運行此代碼后: from selenium.webdriver.support.select import Select driver = webdriver.Chrome() driver.get("https://examplehtml.com/gims/app/reports") ##Report type driver.find_element_by_xpath('//*[@id="reportType"]').send_keys("Power Report")這會插入“Power Report”一詞,但它不會像我手動選擇報告類型那樣選擇和向前移動頁面,我認為這是因為 NoSuchElementException 錯誤。為什么找不到元素以及如何解決此錯誤。我對 Selenium 還很陌生,所以任何建議都會有所幫助。提前致謝!
2 回答

一只名叫tom的貓
TA貢獻1906條經(jīng)驗 獲得超3個贊
我想您需要使用您導(dǎo)入的 Select 類。因此,如果您的 HTML 如下所示:
<select>
<option value="1">Power Report</option>
<option value="2">Other Report</option>
</select>
..然后我會試試這個代碼:
e = driver.find_element_by_xpath('//*[@id="reportType"]')
Select(e).select_by_value('1')
.. 如果下拉元素是 HTML 選擇元素。

夢里花落0921
TA貢獻1772條經(jīng)驗 獲得超6個贊
請確保您要查找的元素可見。Selenium 無法處理隱形元素。如果您想要的元素在下拉菜單中,首先您需要下拉菜單。然后,再次嘗試查找該元素。
添加回答
舉報
0/150
提交
取消