python selenium click打開新窗口


 

 

a = browser.find_elements_by_xpath("//div[@class='xx']")
a.click()

報錯提示: Other element would receive the click

 

改為如下即可:

browser.execute_script("arguments[0].click();", a)

打開新窗口,切換到新窗口,關閉新窗口,切換回舊窗口

handle_main = browser.current_window_handle

a = browser.find_elements_by_xpath("//div[@class='xx']/a[1]")  # a[1]就是第一個a標簽  當前元素下xpath為"./div[1]/a[2]"
browser.execute_script("arguments[0].click();", a)

handle_all = browser.window_handles  # 只有2個窗口時
for h in handle_all:
    if h != handle_main:
        handle_new = h
browser.switch_to.window(handle_new)

browser.close()
browser.switch_to.window(handle_main)

 

 

滾動到指定位置:

js = "window.scrollTo(100, 200)"   # 對應的X Y軸
browser.execute_script(js)

  

在Js中獲取X,Y軸坐標

var box=document.getElementsByClassName('user-header-personal')[0]   // 注意就算只有一個元素 也要寫上[0],js默認為數組

box.getBoundingClientRect().top
box.getBoundingClientRect().left

  

 

參考:https://blog.csdn.net/u012941152/article/details/88418812

https://www.cnblogs.com/yoyoma0355/p/9263227.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM