1

I'm trying to automate a website to automatically make user profiles. Have run into a NG button that I have not be able to select. I have tried XPATH, CSS, and Class Name. None to these seem to work. I keep getting "NoSuchElementException" with:

By.XPATH, '//button[normalize-space()="Create New User"]'

and

By.CSS_SELECTOR, '.newUser.ng-scope'

When I try to use Webdriverwait, I get a time out error.

Code trials:

import time
from webbrowser import Chrome
from xml.etree.ElementPath import find

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome()
driver.get('https://power.dat.com')

chrome_optinons = webdriver.ChromeOptions()
chrome_optinons.add_argument('--headless')
chrome_optinons.add_argument('window-size=1920x1080')

username = driver.find_element(By.XPATH, '//*[@id="mat-input-1"]')
username.send_keys('')

password = driver.find_element(By.XPATH, '//*[@id="mat-input-0"]')
password.send_keys('')

#(time.sleep(3))

loginbutton = driver.find_element(By.XPATH, '//*[@id="submit-button"]')
loginbutton.click()

(time.sleep(5))

profiledrop = driver.find_element(By.XPATH, '//*[@id="user-salutation"]')
profiledrop.click()

Adminbutton = driver.find_element(By.CSS_SELECTOR, '#userPreferencesUl > li:nth-child(5) > a')
Adminbutton.click()

# (time.sleep(10))

NewUsrBtn = driver.find_element(By.XPATH, '//button[normalize-space()="Create New User"]')
NewUsrBtn.click()

#WebDriverWait(driver , 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.newUser.ng-scope'))).click()`

The HTML

<header>
        <ul>
            <!-- ngRepeat: product in office.products --><!-- ngIf: product.seatCount --><li ng-repeat="product in office.products" ng-if="product.seatCount" class="ng-scope">
                <span class="product ng-binding">National Account: Combo Premium Pooling Subscription</span> <span class="count ng-binding">999999841</span> seats out of <span class="total ng-binding">999999999</span> available
            </li><!-- end ngIf: product.seatCount --><!-- end ngRepeat: product in office.products --><!-- ngIf: product.seatCount --><li ng-repeat="product in office.products" ng-if="product.seatCount" class="ng-scope">
                <span class="product ng-binding">National Account: DAT Connexion Subscription</span> <span class="count ng-binding">999999181</span> seats out of <span class="total ng-binding">999999999</span> available
            </li><!-- end ngIf: product.seatCount --><!-- end ngRepeat: product in office.products --><!-- ngIf: product.seatCount --><li ng-repeat="product in office.products" ng-if="product.seatCount" class="ng-scope">
                <span class="product ng-binding">National Account: Power Broker Subscription</span> <span class="count ng-binding">999999182</span> seats out of <span class="total ng-binding">999999999</span> available
            </li><!-- end ngIf: product.seatCount --><!-- end ngRepeat: product in office.products -->
        </ul>
        <h3 class="ng-binding">Total Quality Logistics INC</h3>
        <p>Need help? Customer support is available 4AM-6PM Pacific weekdays and 5AM-12PM Pacific Saturdays at 
<span class="supportPhone">1-800-848-2546.</span></p>
        <!-- ngIf: isTrustedAdmin() -->
<button class="newUser ng-scope" type="button" ng-if="isTrustedAdmin()" ng-click="onNewUser();" ng-disabled="user.isInEditMode">
            Create New User
        </button><!-- end ngIf: isTrustedAdmin() -->

        <button class="manageGroups" type="button" ng-click="onManageGroups();">
            Manage Groups
        </button>
    </header>

I also attached a screen shot of more of the HTML if needed

Full HTML

DaHawg
  • 73
  • 5

1 Answers1

-1

The desired element is an Angular element, so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.newUser.ng-scope[ng-if^='isTrustedAdmin'][ng-click^='onNewUser']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='newUser ng-scope' and contains(., 'Create New User')][starts-with(@ng-if, 'isTrustedAdmin') and starts-with(@ng-click, 'onNewUser')]"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

Update

As a last resort to to click using execute_script() as follows:

  • Using CSS_SELECTOR:

    driver.execute_script("arguments[0].click();", driver.find_element(By.CSS_SELECTOR, "button.newUser.ng-scope[ng-if^='isTrustedAdmin'][ng-click^='onNewUser']"))
    
  • Using XPATH:

    driver.execute_script("arguments[0].click();", driver.find_element(By.XPATH, "//button[@class='newUser ng-scope' and contains(., 'Create New User')][starts-with(@ng-if, 'isTrustedAdmin') and starts-with(@ng-click, 'onNewUser')]"))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • When I run with the CSS Selector or Xpath with the code you provided I still get a TimeOutException – DaHawg Aug 03 '22 at 15:51
  • Don't know if this makes a difference but here is the HTML for the button after I manually click the button: ``` ``` – DaHawg Aug 03 '22 at 16:11
  • The updated HTML I provided was to show the change in HTML script once I manually clicked the button. I am getting a time out error currently. I didn't know if that would be of use at all just noticed it changed once I manually clicked the button. – DaHawg Aug 03 '22 at 19:32
  • Thank you for your help by the way! I have been fighting this one button for a couple of days now. :( Hopefully can get it working soon. – DaHawg Aug 03 '22 at 19:42
  • Instead of _HTML once I manually clicked the button_, update the question with the HTML of the element before you invoke click on it. – undetected Selenium Aug 03 '22 at 19:52
  • The HTML in the question is from before I click the button. I added the HTML after clicking the button in the comments. The only difference between the two is disabled="disabled" which I know realize just disables the button. It is almost like a drop down button that just expands could that be a reason it is timing out since it does not actually change screens. – DaHawg Aug 03 '22 at 20:07
  • Only `disabled="disabled"` gets added after the click, and I haven't used the `disabled` attribute. So the locators should still work. – undetected Selenium Aug 03 '22 at 20:12
  • I think it is locating the button but it is throwing a TimeoutException instead of clicking the button. – DaHawg Aug 03 '22 at 20:20
  • With your updated paths it throws a NoSuchElementException – DaHawg Aug 03 '22 at 20:25
  • Is there any parent element of the `` tag? – undetected Selenium Aug 03 '22 at 20:53
  • I don't see a parent class. I just updated the HTML in the question with the full header that includes the button. It starts with – DaHawg Aug 03 '22 at 21:03
  • Does this element opens on clicking some other element? Possibly in a different window which happens incase of Angular Applications? – undetected Selenium Aug 03 '22 at 21:54
  • I am not sure I am fully understanding your question but let me see if this helps. Once logged in I have to click a drop-down window and then click administration. That then opens a new window that takes me to power.dat.com/admin/office. Then from there, it gives me two boxes that "Create new user" and "Manage Groups". The create new user button is where it won't click. Is the opening the new window where it is no longer connecting to the script? – DaHawg Aug 03 '22 at 22:09
  • So _`That then opens a new window`_ is the issue :) you have to [switch to the new window](https://stackoverflow.com/questions/53690243/selenium-switch-tabs/53692407#53692407) first. – undetected Selenium Aug 03 '22 at 22:15
  • 1
    That fixed it! Thank you so much! I was about to lose my mind! – DaHawg Aug 03 '22 at 23:06
  • Glad to be able to help you. – undetected Selenium Aug 03 '22 at 23:07