Selenium Python按名称查找元素无效

时间:2018-06-27 15:48:52

标签: python html selenium testing pycharm

我正在使用PyCharm和Selenium网络驱动程序在多个字段中测试网络表单。

网页上有两个类似的电话和电子邮件DIV:

<div class="popup-subscribe-new__content">
                        <div class="field field_type_email js-field-custom-email js-field">
                            <input type="phone" name="phone" placeholder="Phone Number" autocomplete="off" required="required" class="field__input js-field-input js-inputmask-phone" pattern="^[0-9\+\-\( \)]+$" data-trim="true" pattern-flags="i">
                            <div class="field__error">Enter the phone number</div>
                        </div>
                    </div>

<div class="popup-subscribe-new__content">
                    <div class="field field_type_email js-field-custom-email js-field">
                        <input type="email" name="email" placeholder="E-mail" autocomplete="off" required="required" class="field__input js-field-input" pattern="^([A-Za-z0-9_\.\+\-])+@([A-Za-z0-9_\.\-])+\.([A-Za-z]{2,4})$" data-trim="true">
                        <div class="field__error">Enter the e-mail</div>
                    </div>
                </div>

在测试中,我使用 phone_field = driver.find_element_by_name('phone')进行电话搜索 和 email_field = driver.find_element_by_name('email')用于电子邮件搜索

结果是,测试成功找到了电话字段,但是在尝试查找电子邮件字段之后,PyCharp回答了 ElementNotVisibleException:消息:元素不可见

我在哪里错了? 谢谢, 尤金

2 个答案:

答案 0 :(得分:0)

尝试等到元素可见后

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.NAME, "email")))
email_field = driver.find_element_by_name('email')

有关waits的更多信息可以在文档here中找到。

答案 1 :(得分:0)

您可以尝试以下代码:

wait = WebDriverWait(browser, 30) 

phone_field = driver.find_element_by_name('phone')  
phone_field.send_keys('some phone number')
email_field = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div[class*='js-field-custom-email']>input[name='email']')))  

email_field.send_keys("""some@email.com"""")  

确保导入以下内容:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
相关问题