问题
I'm currently trying to locate Microsoft Sign In page's email input box by using xpath (others as well) but after many tries I still can't locate the correct element for it.
After copying the element from the page, this is the element given:
<input type="email" class="form-control" placeholder="Email, phone, or Skype" aria-required="true" spellcheck="false" autocomplete="off" data-bind="
hasFocus: focus,
textInput: email,
attr: {'placeholder': config.text.emailPlaceHolder, 'aria-invalid': !error}">
And this is currently my python code:
login = browser.find_element_by_xpath("//input[@class='form-control']")
login.send_keys(config.username)
login.send_keys(Keys.RETURN)
I had tried multiple times but I still can't get the proper element to proceed. After entering https://forms.office.com/ I had successfully captured the sign in element but stuck at the next page.
回答1:
As the the desired element is within an <iframe> so to invoke click() on the element you have to:
- Induce WebDriverWait for the desired frame to be available and switch to it.
- Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using
XPATH:WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@class='landing-signin-hrd' and @id='hrdIframe']"))) login = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='form-control' and @placeholder='Email, phone, or Skype']"))) login.send_keys("StrangerSphinx") login.send_keys(Keys.RETURN)Using
CSS_SELECTOR:WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe.landing-signin-hrd#hrdIframe"))) login = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control[placeholder='Email, phone, or Skype']"))) login.send_keys("StrangerSphinx") login.send_keys(Keys.RETURN)
Reference
You can find a relevant detailed discussion in Ways to deal with #document under iframe
来源:https://stackoverflow.com/questions/60237336/cant-locate-element-on-microsoft-sign-in-page