2

I'm running a script on vodafone.co.uk, but I can't find any element using the CSS Selector.

 browser.get("http://freesim.vodafone.co.uk/")
 browser.find_element_by_css_selector("#frmNew > div:nth-child(32) > div > div > div > div.freesim-text-last.last > button > span").click()

And this is what I get:

NoSuchElementException: Message: Unable to locate element

Doesn't matter what CSS I use, selenium doesn't find any element anyways.

3
  • "Doesn't matter what CSS I use". This particular css locator would not find any element in this particular page indeed. Since there is no #frmNew can be found (try to use simple search across the page source). Share what other CSS so not work.
    – Alexey R.
    Commented Jun 16, 2018 at 22:00
  • #ibSubmit2 This one here, for example. These are all CSS selectors I'm getting directly from the page. I've just noticed that on other CSS selectors ( such as "#notlisted"; "#txtEmail" and "#txtMobile") and I get "can't access dead object". Thanks for the reply.
    – Beter
    Commented Jun 16, 2018 at 22:05
  • Never mind, my bad. The "can't access dead object" is due to another command I input right before, that was made to switch to another frame. If that command doesn't get used, also those CSS selectors return back the "Unable to locate element" error
    – Beter
    Commented Jun 16, 2018 at 22:11

2 Answers 2

1

The beginning of the CSS Selector is wrong.

Searching for .freesim-text-last button span you will find two elements.

Vodafone site

Pick the one which you're trying to reach using the follow:

.freesim-text-last button span:nth-of-type(1)

for the first element and

.freesim-text-last button span:nth-of-type(2)

for the second element.

2
  • I don't know whether I'm running into some stupid mistakes or not, but even these two selectors wont work. As I said in another comment, all the CSS selectors I've tested won't work for some reason. Do you think it might have something to do with frames?
    – Beter
    Commented Jun 17, 2018 at 0:11
  • Are you able to navigate on other sites correctly? Commented Jun 18, 2018 at 0:48
1

In situations like this, you should try working up and seeing if the more easily available elements can be found instead. A fairly standard way check is to confirm the title element is found and correct.

Afterwards I would consider a timing issue. Has the DOM fully loaded before searching for this element? Selenium comes with different waits (implicit, explicit) which you can use to wait for certain elements to be loaded. A quick (and usually bad) way of testing this would just be to add:

time.sleep(5)

After you get open the browser, and before the element you're looking for, to give it time to load.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.