代码
from selenium import webdriver
import time
def main():
chrome_driver = 'C:\xxxxx\Google\Chrome\Application\chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver)
driver.get("https://www.douban.com/")
time.sleep(5)
curFrame = driver.find_element_by_tag_name("iframe")
#print(curFrame)
time.sleep(5)
#登录表单在页面的框架中中,所以要切换到该框架
driver.switch_to_frame(curFrame)
# 通过使用选择器选择到表单元素进行模拟输入和点击按钮提交
driver.find_element_by_class_name('account-tab-account').click()
time.sleep(5)
driver.find_element_by_id('username').send_keys('xxx')
driver.find_element_by_id('password').send_keys('xxx')
driver.find_element_by_css_selector('.account-form-field-submit>.btn').click()
time.sleep(10)
#去指定页面,删除小组中的回应
driver.find_element_by_link_text("小组").click()
time.sleep(5)
#上一次点击操作,打开了新页面,现在需要在新页面定位元素,否则会出现定位不到的问题,0是前面一个页面,1是点击后的页面
driver.switch_to.window(driver.window_handles[1])
time.sleep(2)
#driver.find_element_by_css_selector(".mod.profile-entry>.info>a").click()
driver.find_element_by_link_text("我的小组主页").click()
time.sleep(5)
driver.find_element_by_link_text("回应").click()
time.sleep(5)
tableBody = driver.find_element_by_class_name('olt')
list = tableBody.find_elements_by_class_name("title")
print(len(list))
for item in list:
tagetItem = item.find_element_by_tag_name("a").text
print(tagetItem)
#item.find_element_by_tag_name("a").click()
time.sleep(5)
driver.quit()
if __name__== '__main__':
main()
知识点:弹出一个新窗口的元素定位
selenium中webdriver跳转新页面后定位置新页面的两种方式-参考博文