超高单击元素

2024-04-19 09:16:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经尝试了所有的方法在seleniums文档中找到元素,但是我想不出来。谢谢你的帮助

def protonmail_signup():
    browser.get('https://protonmail.com/signup')
    time.sleep(random.randint(10,15))
    free_acc = browser.find_element_by_class_name('panel-heading').click()
    time.sleep(random.randint(2,3))
    free_elem = browser.find_element_by_id('freePlan').click()
    time.sleep(random.randint(20,25))
    email_elem = browser.find_element_by_id('username').click()

一切正常,除了:

email_elem = browser.find_element_by_id('username').click()

Tags: browseridfreebytimeemailsleeprandom
1条回答
网友
1楼 · 发布于 2024-04-19 09:16:43

看起来usernameiframe元素中。您需要切换到iframe,然后输入用户名文本:

# save main frame context so we can switch back later
main_frame = driver.current_window_handle

# Get the first iframe   there are two
iframe = driver.find_elements_by_xpath("//iframe[@title='Registration form']")[0]

driver.switch_to.frame(iframe)

# enter email
email_elem = browser.find_element_by_id('username').click()

然后,您需要切换回主框架以输入密码:

# switch back to main frame
driver.switch_to_window(main_frame)

#enter password next

正如我在评论中提到的,有两个嵌套的iframe元素。第一个[0]将获得包含用户名的iframe。第二个,您需要切换到,以便单击Create Account

second_iframe = driver.find_elements_by_xpath("//iframe[@title='Registration form']")[1]

driver.switch_to.frame(second_iframe)

# click Create Account button

相关问题 更多 >