0

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

2 Answers 2

2

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:

Sign up to request clarification or add additional context in comments.

1 Comment

<a class="sn-widget-list-item sn-widget-list-item_hidden-action module-node" id="2ccb50dfc61122820032728dcea648fe" href="task_list.do?sysparm_userpref_module=2ccb50dfc61122820032728dcea648fe&amp;sysparm_query=assignment_group=javascript:getMyGroups()^active=true^assigned_to=^sys_class_name!=cert_follow_on_task^sys_class_name!=sc_req_item^sys_class_name!=sc_request^EQ&amp;sysparm_clear_stack=true" target="gsft_main"> <div class="sn-widget-list-content" data-id="2ccb50dfc61122820032728dcea648fe"> <div class="sn-widget-list-title">My Groups Work</div></div></a> I want to access My Groups Work
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.