Python selenium2 如何获取动态ID呢
怪我咯
怪我咯 2017-04-18 09:55:58
0
1
587

登陆126邮箱时 通过几种定位都不行 真蛋疼

            代码如下:
            

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('http://126.com')
frame = driver.find_element_by_id('x-URS-iframe')
driver.switch_to.frame(frame)
'''id定位 时时变因为它的ID是动态的'''
driver.find_element_by_id('auto-id-1479158591510').click()
'''通过类名定位也定不到'''
driver.find_element_by_class_name('j-inputtext dlemail').click()
'''路劲定位也定不到 '''
driver.find_element_by_xpath('//[@id ="auto-id-1479158591510"]').click()*

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(1)
PHPzhong

Use css selector to solve this problem. The code is as follows. I hope it can solve your problem

from selenium import webdriver
import time
driver = webdriver.Chrome("/Users/lvxiang/Downloads/chromedriver")
driver.get('http://www.126.com/')
frame = driver.find_element_by_id('x-URS-iframe')
driver.switch_to.frame(frame)
# XXXX替换为你的用名和密码
driver.find_element_by_css_selector("form input[name='email']").send_keys("XXXX")
time.sleep(1)

driver.find_element_by_css_selector("form input[name='password']").send_keys("XXXX")
time.sleep(1)
driver.find_element_by_id("dologin").click()
print("登录成功")
time.sleep(1)
driver.close()
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template