1

I am trying to select

<div ng-click="select('expert')" class="select-exp__item select-exp__item_active" ng-class="{'select-exp__item_active': 'expert'===selected}" style="">

I have tried a few things

driver.find_element_by_css_selector("div[ng-click='select('expert')']").click()

driver.find_element_by_class_name('select-exp__item-title').click()

WebDriverWait(driver, 10).until(EC.element_to_be_clickable(By.XPATH("//div[@class='select-exp__item select-exp__item_active']").click()

WebDriverWait(driver, 10).until(EC.element_to_be_clickable(By.XPATH("//div[@class='select-exp__item select-exp__item_active' and contains(@ng-click, 'select('expert')')]").click()

...but I get the same error each time:

InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified

Grateful for any guidance, have googled "selenium ng-click" extensively without much luck and still learning Selenium, thank you.

Here's the full HTML:

<div class="agmodal__wrapper agmodal onboarding-wrapper ng-scope agmodal__wrapper--visible" ng-controller="OnboardingController as ctrl" ng-class="{'agmodal__wrapper--visible': ctrl.shown}" tabindex="-1" style="">
    <div class="onboarding">
        <div class="onboarding__body">
            <!-- ngIf: ctrl.onboardingService.step === 1 --><section class="select-exp step ng-scope" ng-if="ctrl.onboardingService.step === 1" style="">
    <div class="select-exp__header step__header">
        <div class="select-exp__title step__title">Welcome to AMZScout PRO Extension</div>
        <div class="select-exp__desc step__desc">
            PRO Extension helps you find the perfect product to sell on Amazon
        </div>
    </div>
    <div class="select-exp__body">
        <div class="select-exp__body-title">
            Select your experience level with Amazon</div>
        <div class="select-exp__body-desc">
            We will adjust the amount of pop up tips and certain parameters based on your experience. You will be able
            to
            change this choice later in the Settings.</div>
        <div class="select-exp__items">
            <div ng-click="select('beginner')" class="select-exp__item" ng-class="{'select-exp__item_active': 'beginner'===selected}">
                <div class="select-exp__item-title">Beginner</div>
                <div class="select-exp__item-desc">If you are new to Amazon research, you’ll see vital parameters that
                    will help you choose a great
                    product quickly. Great for learning.</div>
            </div>
            <div ng-click="select('expert')" class="select-exp__item select-exp__item_active" ng-class="{'select-exp__item_active': 'expert'===selected}" style="">
                <div class="select-exp__item-title">Expert</div>
                <div class="select-exp__item-desc">As an expert, you’ll see the full data and be able to fine-tune your
                    research.</div>
            </div>
        </div>
    </div>

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Alexis
  • 89
  • 2
  • 9

2 Answers2

1

Try located the element with css selector like this:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.select-exp__item.select-exp__item_active'))).click()

You are wrong in using WebdriverWait, your code:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable(By("value").click()

It's should like this pattern:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By, 'value'))).click()

Explicit Waits

frianH
  • 7,295
  • 6
  • 20
  • 45
1

This error message...

InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified

...implies that the selector which you have used isn't a valid selector expression.


Analysis

As per your first code trial:

driver.find_element_by_css_selector("div[ng-click='select('expert')']").click()

( and ) have a special effect when used within a . So the expression stands invalid. You need to escape the characters.


Solution

As the desired element is an Angular element you need to use WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.select-exp__item.select-exp__item_active[ng-click=\"select('expert')\"]"))).click()
    
  • XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='select-exp__item select-exp__item_active' and @ng-click=\"select('expert')\"]"))).click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352