search

Home  >  Q&A  >  body text

Python selenium2 如何获取动态ID呢

登陆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()*

怪我咯怪我咯2767 days ago589

reply all(1)I'll reply

  • PHPz

    PHPz2017-04-18 09:57:58

    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()

    reply
    0
  • Cancelreply