亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

selenium.common.exceptions.InvalidSelector

selenium.common.exceptions.InvalidSelector

慕妹3146593 2021-10-19 10:18:09
我正在嘗試編寫一個腳本來處理具有多個元素的網頁。單擊此元素后將打開一個新窗口。但是我的腳本在識別元素方面存在問題。我需要幫助來定位元素并處理多個窗口我嘗試使用 Chrome 查找 Xpath,但它在 Internet Explorer 中不一樣。我也試過使用 CSS 選擇器它不起作用。說是無效的。函數 test_google_search_page 的代碼: def test_google_search_page(self): driver=self.driver driver.get(" http://xxxx.com ") str1=driver.title print(str1)    #get the window handles using window_handles    window_before=driver.window_handles[0]    print(window_before)    #driver.find_element_by_xpath("//*                   [@id='2ccb50dfc61122820032728dcea648fe']/div/div")    driver.find_element_by_css_selector("#\32  ccb50dfc61122820032728dcea648fe > div > div")    window_after=driver.window_handles[1]    driver.switch_to.window(window_after)    str2=driver.title    print(str2)    print(window_after)    self.assertNotEqual(str1,str2)    print('This window has a different title')    driver.switch_to.window(window_before)    self.assertEqual(str1,driver.title)    print('Returned to parent window. Title now match')ERROR: test_google_search_page (__main__.GoogleOrgSearch)----------------------------------------------------------------------Traceback (most recent call last):  File "C:\Users\PSWN672P\AppData\Local\Programs\Python\Python37\Python programs\SNOW2.py", line 21, in test_google_search_page    driver.find_element_by_css_selector("#\32 ccb50dfc61122820032728dcea648fe > div > div")  File "C:\Users\PSWN672P\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 598, in find_element_by_css_selector    return self.find_element(by=By.CSS_SELECTOR, value=css_selector)  File "C:\Users\PSWN672P\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element    'value': value})['value']
查看完整描述

2 回答

?
ABOUTYOU

TA貢獻1812條經驗 獲得超5個贊

您的#\32 ccb50dfc61122820032728dcea648fe > div > divCSS 選擇器確實無效。請參考CSS 選擇器語法規范。

你的意思是:#2ccb50dfc61122820032728dcea648fe > div > div?盡管如此,如果不查看您試圖定位的頁面和元素的 HTML 源代碼,就不可能為您提供特定的正確選擇器。

2ccb50dfc61122820032728dcea648feID本身雖然看起來自動生成的,你應該尋找替代定位器來獲得所需的元素

查看完整回答
反對 回復 2021-10-19
?
慕碼人8056858

TA貢獻1803條經驗 獲得超6個贊

這個錯誤信息...


driver.find_element_by_css_selector("#\32 ccb50dfc61122820032728dcea648fe > div > div")

.

selenium.common.exceptions.InvalidSelectorException: Message: An invalid or illegal selector was specified

...暗示CssSelector不是有效的。


在存在相關 HTML 的情況下構建最適合的CssSelector會更容易。但是,根據您的代碼試驗:


\32因為Id的值看起來不正確。

ccb50dfc61122820032728dcea648fe是動態生成的內容。所以也不能用。

在這里你可以找到CSS 選擇器參考


查看完整回答
反對 回復 2021-10-19
  • 2 回答
  • 0 關注
  • 507 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號