我正在尝试编写一个脚本来处理具有多个元素的网页。单击该元素将进入一个新窗口。但是我的脚本在识别元素方面存在问题。我需要帮助来定位元素并处理多个窗口
我尝试使用Chrome找到Xpath,但在Internet Explorer中却不一样。我也尝试过使用CSS选择器,但它不起作用。说这是无效的。
函数test_google_search_page的代码: def test_google_search_page(自己): driver = self.driver driver.get(“ http://xxxx.com”) str1 = driver.title 打印(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']
File "C:\Users\PSWN672P\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\PSWN672P\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: An invalid or illegal selector was specified
答案 0 :(得分:2)
您的#\32 ccb50dfc61122820032728dcea648fe > div > div
CSS选择器实际上是无效。请参阅the CSS selector grammar specification。
您是说:#2ccb50dfc61122820032728dcea648fe > div > div
?即使如此,也无法在没有看到您要查找的页面和元素的HTML源代码的情况下为您提供特定的正确选择器。
2ccb50dfc61122820032728dcea648fe
ID本身虽然看起来是自动生成的,但您可能应该寻找替代的定位符才能找到所需的元素,本主题可能有助于您了解如何使用硒来定位元素:>
答案 1 :(得分:0)
此错误消息...
eg.
I have table Config of DB1:
+--------+----------+
| ID | Status |
+--------+----------+
| 1 | Active |
+--------+----------+
| 2 | Inactive |
+--------+----------+
| 3 | Active |
+--------+----------+
Here I have my DB2 with table Config
+--------+----------+
| ID | Status |
+--------+----------+
| 1 | Active |
+--------+----------+
| 2 | Active |
+--------+----------+
| 3 | Active |
+--------+----------+
| 4 | Inactive |
+--------+----------+
Output should be after the insertion of table Config data from DB2 to DB1:
+--------+----------+
| ID | Status |
+--------+----------+
| 1 | Active |
+--------+----------+
| 2 | Active |
+--------+----------+
| 3 | Active |
+--------+----------+
| 4 | Inactive |
+--------+----------+
...表示 CssSelector 无效。
在存在相关HTML的情况下,构造最适合的 CssSelector 会更容易。但是,根据您的代码试用:
driver.find_element_by_css_selector("#\32 ccb50dfc61122820032728dcea648fe > div > div")
.
selenium.common.exceptions.InvalidSelectorException: Message: An invalid or illegal selector was specified
,因为 Id 的值看起来不正确。\32
是动态生成的内容。因此它也无法使用。在这里您可以找到CSS Selector Reference