Reputation: 11
I am trying to write a script to handle web pages that has multiple elements. This element once clicked will lead to a new window. But my script has problem in identifying the element. I need help to locate element and handle multiple windows
I tried finding Xpath by using Chrome but it is not the same in Internet Explorer. I also tried using CSS selector it doesn't work. Says it is invalid.
Code for the function 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']
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
Upvotes: 0
Views: 18395
Reputation: 193088
This error message...
driver.find_element_by_css_selector("#\32 ccb50dfc61122820032728dcea648fe > div > div")
.
selenium.common.exceptions.InvalidSelectorException: Message: An invalid or illegal selector was specified
...implies that the CssSelector wasn't a valid one.
It would been easier to construct the best fit CssSelector in presence of the relavent HTML. However as per your code trials:
\32
as the value of the Id looks incorrect.ccb50dfc61122820032728dcea648fe
is a dynamically generated content. So it can't be used as well.Here you can find the CSS Selector Reference
Upvotes: 0
Reputation: 473863
Your #\32 ccb50dfc61122820032728dcea648fe > div > div
CSS selector is indeed invalid. Please refer to the CSS selector grammar specification.
Did you mean: #2ccb50dfc61122820032728dcea648fe > div > div
? Even though, it's impossible to give you a specific correct selector without seeing the HTML source of the page and element you are trying to locate.
The 2ccb50dfc61122820032728dcea648fe
id itself though looks auto-generated, you should probably look for alternative locators to get to the desired element, this topic might help to get an idea of how to approach locating elements with selenium:
Upvotes: 2