簡體   English   中英

如何使用 python selenium 點擊多個雅虎標簽

[英]how to click on multiple yahoo tag using python selenium

如何使用 selenium 單擊雅虎搜索結果的多個標簽。 例如,我搜索狀態並顯示多個結果。 我想點擊每個鏈接>這里是 xpath 鏈接的樣子

xpath
#//*[@id="yui_3_10_0_1_1607785449630_1057"]
#//*[@id="yui_3_10_0_1_1607785449630_1057"]
#//*[@id="yui_3_10_0_1_1607785449630_1189"]

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.webdriver.firefox.options import Options
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


for i in range(1):
    sleep(2)
    driver = webdriver.Firefox()

    driver.get("http://www.yahoo.com/")
    search = driver.find_element_by_id("ybar-sbq")
    my_choice = random.choice(["Atos stock"])
    search.send_keys(my_choice)
    sleep(2)
    pg.press('enter')
    sleep(4)
    for i in range(1):
        try:
    
            #//*[@id="yui_3_10_0_1_1608697269369_685"]
            sleep(2)
            driver.find_elements_by_xpath('//*[@id^="yui_3"]')

            #driver.find_elements_by_xpath("//*[@id^='yui_3']").click()
            sleep(2)
            driver.execute_script("window.scrollBy(0, 10)")
            sleep(2)
            driver.execute_script("window.scrollBy(0, 50)")
            sleep(2)
            driver.execute_script("window.scrollBy(0, 100)")
            sleep(5)
            #driver.back()
            print('done')
            sleep(2)
        except:
            print('error')
            continue
driver.close()
driver.quit()

這應該得到所有以 yui_3 開頭的元素。

elems=driver.find_elements_by_css_selector("[id^='yui_3']")
print(len(elems))

現在,當您單擊它們時會發生什么可能會使元素陳舊或不陳舊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM