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

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


#python #css #selenium

4 Likes111.55 GEEK