简体   繁体   中英

Selenium - switch to div class that is a window

I need help selecting an element on a webpage with Selenium. I have been using Selenium on this website for about 3 weeks and so far, I can usually find an element by css selector or XPath. However, this specific section of the website is giving me a very hard time. After I click on “reset office 365 password” a window comes up and I want to programmably put in the new password but it can't find anything in the popup window. Here is what the page looks like: (I am too low of score to post pictures here) https://cdn.discordapp.com/attachments/768594779344470022/845811910577881098/unknown.png

Here is the whole element's information:

<input type="password" tabindex="1" name="password" class="m-third pass ng-pristine ng-empty ng-invalid ng-invalid-required ng-touched" ng-model="password.value" ng-blur="password.check = false" ng-focus="password.check = true" required="" autofocus="" ng-disabled="!active">

Here is what I tried: (I tried a lot of things)

Tried clicking on the password box by using css selector – failed: Invalid selector

im_blacklistaddbutton = browser_options.browser.find_element_by_css_selector('#ng-app > div.page-container > div > div > div.vertical-tabs.j-vertical-tabs.ng-scope > div.vertical-tabs-panes.p0 > div > div > div.page-content.ng-scope > div > div > form > div > div > div.ng-isolate-scope > div.modal > div.modal-body.ng-transclude > div > reset:password > ng-form > div:nth-child(1) > div > div.validation-input > input')
 im_blacklistaddbutton.send_keys(email_pd.pd)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified

Tried clicking on the password box by using xpath selector – failed: Namespace Error

im_blacklistaddbutton = browser_options.browser.find_element_by_xpath('//*[@id="ng-app"]/div[2]/div/div/div[3]/div[2]/div/div/div[2]/div/div/form/div/div/div[3]/div[1]/div[2]/div/reset:password/ng-form/div[1]/div/div[1]/input')
im_blacklistaddbutton.send_keys(email_pd.pd)
NamespaceError: Failed to execute 'evaluate' on 'Document': The string '//*[@id="ng-app"]/div[2]/div/div/div[3]/div[2]/div/div/div[2]/div/div/form/div/div/div[3]/div[1]/div[2]/div/reset:password/ng-form/div[1]/div/div[1]/input' contains unresolvable namespaces.

Tried waiting for the element by partial link text: It timed out

wait.until(EC.visibility_of_element_located((By.PARTIAL_LINK_TEXT, 'Generate password')))
selenium.common.exceptions.TimeoutException: Message:

Tried waiting for the element by ID name text value: It timed out

wait.until(EC.text_to_be_present_in_element((By.CLASS_NAME, 'btn m-link'), "Generate Password"))
selenium.common.exceptions.TimeoutException: Message:

Tried to switch to a window or iframe but it said that the div class of "model" is not a window or an iframe.

From here I am completely lost as to why this stupid window is not accessible. Text window - why are you so mean to me?

Here is my specific function in total:

def reset_im_oa_password():
    browser_options.browser.get('https://cpx.intermedia.net/ControlPanel/Menu/AccountMenu/?frameUrl=https://cpx.intermedia.net/aspx/Office365/Home/licenses#/installed/users')
    wait = WebDriverWait(browser_options.browser, 10)
    try:
        wait.until(EC.element_to_be_clickable((By.XPATH, 'player')))
    except exceptions.TimeoutException as e:
        pass

    browser_options.browser.switch_to_frame('mainFrame')
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#ng-app > div.page-container > div > div > div.vertical-tabs.j-vertical-tabs.ng-scope > div.vertical-tabs-panes.p0 > div > div > div.page-content.ng-scope > div > div > form > div > div > div:nth-child(2) > div.table-wrap.table-fixed.j-table-wrap.s-wide.ng-isolate-scope > div.table-filter > div.table-filter-search.searchbox.ng-isolate-scope > div > span:nth-child(3) > input')))
    im_blacklistaddbutton = browser_options.browser.find_element_by_css_selector('#ng-app > div.page-container > div > div > div.vertical-tabs.j-vertical-tabs.ng-scope > div.vertical-tabs-panes.p0 > div > div > div.page-content.ng-scope > div > div > form > div > div > div:nth-child(2) > div.table-wrap.table-fixed.j-table-wrap.s-wide.ng-isolate-scope > div.table-filter > div.table-filter-search.searchbox.ng-isolate-scope > div > span:nth-child(3) > input')
    im_blacklistaddbutton.send_keys(email_or_user_selection.email_select)
    im_blacklistaddbutton = browser_options.browser.find_element_by_css_selector('#ng-app > div.page-container > div > div > div.vertical-tabs.j-vertical-tabs.ng-scope > div.vertical-tabs-panes.p0 > div > div > div.page-content.ng-scope > div > div > form > div > div > div:nth-child(2) > div.table-wrap.table-fixed.j-table-wrap.s-wide.ng-isolate-scope > div.table-filter > div.table-filter-search.searchbox.ng-isolate-scope > div > span:nth-child(3) > button')
    im_blacklistaddbutton.send_keys(Keys.ENTER)
    wait.until(EC.element_to_be_clickable((By.XPATH, ("//*[starts-with(@id, 'btnResetPassword')]"))))
    im_blacklistaddbutton = browser_options.browser.find_element_by_xpath(("//*[starts-with(@id, 'btnResetPassword')]"))
    im_blacklistaddbutton.send_keys(Keys.ENTER)
    try:
        wait.until(EC.visibility_of_element_located((By.PARTIAL_LINK_TEXT, 'Generate password')))
    except exceptions.TimeoutException as e:
        pass
    browser_options.browser.switch_to_window('model') # anything past this section will fail
    wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'model')))
    im_blacklistaddbutton = browser_options.browser.find_element_by_xpath('//*[@id="ng-app"]/div[2]/div/div/div[3]/div[2]/div/div/div[2]/div/div/form/div/div/div[3]/div[1]/div[2]/div/reset:password/ng-form/div[1]/div/div[1]/input')
    im_blacklistaddbutton.send_keys(email_pd.pd)
   
    return

if anyone needs the full code from the webpage let me know. Thanks

If this element is not really inside an iframe as you write, then, wait for it to become clickable, like this:

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='password']")))
im_blacklistaddbutton = browser.find_element_by_css_selector("input[type='password']")
im_blacklistaddbutton.send_keys("new_password")

But make sure that css selector input[type='password'] is unique. If not, try this one: .validation-input>input[type='password'] (Check validation-input class name si correct as it is cut on your screenshot)

  1. If the input frame is inside iframe nothing will work until you switch to this iframe.

Because of no webpage code, right now I can't say why the element is not detectable by Selenium but you can try one thing. Right click on the element(input tag in dom shown in picture) and go to "Copy to" option and select "Copy JS Path". Then go to console tab in dev tools and paste it. Then try to set it's value to some dummy text and see if it sets the password.

jsPath.value="some password"  //this should set the password

If this works, then you can set the value by using JavaScript executor of Selenium in the same way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM