iT邦幫忙

1

使用 selenium _ select 功能,可以定位到select,但是選擇時出現錯誤。

  • 分享至 

  • twitterImage

我是Python自學新手,想自己寫一個自動化程式,但是在選擇選單時發生問題,找尋很多資料但是沒有得到解答,希望有大大可以幫助。/images/emoticon/emoticon13.gif


錯誤碼

Traceback (most recent call last):
File "", line 1, in
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\select.py", line 99, in select_by_index
for opt in self.options:
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\select.py", line 47, in options
return self._el.find_elements(By.TAG_NAME, 'option')
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 685, in find_elements
{"using": by, "value": value})['value']
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: stale element not found
(Session info: chrome=87.0.4280.88)


https://ithelp.ithome.com.tw/upload/images/20201216/20133388caSCiH3isq.png


Python

Selector_Element = Select(driver.find_element_by_tag_name("select"))
Selector_Element.select_by_index(2)


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
2
listennn08
iT邦高手 5 級 ‧ 2020-12-16 16:57:34
最佳解答

找了一下方法 因為選半天選不到
所以我改用 ActionChains

# 要載入 ActionChans 跟 Keys
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains 

# ...

# 設定要選取的 select
select = driver.find_element_by_xpath('//*[@id="contents"]/div/div/div[4]/div[2]/div[3]/div[2]/div[6]/div[2]/div/div/div/div/div/select')
# 設定向下按的 action
action_key_down = ActionChains(driver).move_to_element(select).key_down(Keys.DOWN).key_up(Keys.DOWN)
# 設定按 Enter 的 action
action_key_enter = ActionChains(driver).move_to_element(select).key_down(Keys.RETURN)
# 假設要選優惠券的話 執行 2 次向下
action_key_down.perform()
action_key_down.perform()
# 執行 Enter
action_key_enter.perform()
lulu_meat iT邦研究生 5 級 ‧ 2020-12-16 17:02:23 檢舉

太神了!!!

感謝大神/images/emoticon/emoticon32.gif

那可以知道為什麼選不到位置嗎,他不是有name可以去找,但是卻沒辦法找到正確位置?

line 的話網頁應該都是用 Vue 做的
所以會有很多 virtual dom
我其實是覺得選不到很奇怪
因為可以選到 select 選不到 option
所以有可能是因為 option 是動態產生的導致選不到
但我也不能跟你確定一定是這個原因
因為其他 virtual dom 也有被選到/images/emoticon/emoticon06.gif

0
lulu_meat
iT邦研究生 5 級 ‧ 2020-12-16 13:26:18

你可以看一下selenium的中文文檔
裡面用name定位的用法只有

driver.find_element_by_name('定位元素')

看你的網頁HTML也沒有叫做'select'的元素
補充: 定位選單不是用select喔! 應該要用id(或用name都可以)
所以要改成

driver.find_element_by_id('6736hgrob7u')

你說你想定位選單,建議你用相對位置的定位法

driver.find_element_by_xpath("/html/body/option[2]")

上面那句要依照你要爬的tag改,先用我打的試試看

看更多先前的回應...收起先前的回應...

感謝大大的回應,因為他的id每次都會更新,所以我用name去定位,位址應該有定位到,但問題出在定位Select後,要選擇選項時發生錯誤,我有看HTML裡面沒有frame,我有用測試網頁可以順利執行,所以不知道此網頁在select上有甚麼問題。

Selector_Element =Select(driver.find_element_by_name('typeId'))
Selector_Element.select_by_index(2)

https://ithelp.ithome.com.tw/upload/images/20201216/20133388AaEAoi2fih.png

有網址可以讓我測試嗎

listennn08

這個是Line official Account裡面的選單,需要登入帳號才能進去操作。Line official Account(https://account.line.biz/login?redirectUri=https%3A%2F%2Fmanager.line.biz%2Faccount%2F%40437eexcg%2Fautoresponse%2Fsimple-qa)

你的 line official account 的畫面我看不到
是圖文選單那個頁面嗎?

lulu_meat iT邦研究生 5 級 ‧ 2020-12-16 14:36:05 檢舉

sorry 我眼殘
用xpath的方法也不行嗎?

listennn08
感謝大大測試/images/emoticon/emoticon02.gif

多頁訊息/影像/動作
https://ithelp.ithome.com.tw/upload/images/20201216/2013338829jIiXy5Zm.png

lala_meat

選單位置有找到,但是要去選擇選項的時候出現問題,跟我上面回復的內容一樣。

0
PeggyC
iT邦新手 5 級 ‧ 2020-12-16 14:48:03

感謝回應

我要發表回答

立即登入回答