Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python selenium单击多个yahoo标记_Python_Selenium - Fatal编程技术网

如何使用python selenium单击多个yahoo标记

如何使用python selenium单击多个yahoo标记,python,selenium,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 i

如何使用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))

现在,当您单击这些元素时会发生什么情况,可能会使元素过时或不过时。

您的程序到底挂起在哪里?目前尚不清楚。您是否能够成功单击链接,然后很难返回
?或者您的问题是通过id
yui_3^
获取元素。也许可以查看这篇文章,问题是通过id yui_3^获取元素。该链接很有用,但没有特定的类名。所有id都以#//*[@id]开头="yui_3_10_0_1_这是唯一重复的项目。@JeanS这听起来像是一个问题。与其寻求解决问题的帮助,不如编辑你的问题,询问实际问题。你想做什么?@DebanjanB只是试图在搜索结果后获取链接,然后从雅虎点击。与bing、google或google相比,雅虎有着奇怪的标签其他搜索引擎如何在获得搜索结果后单击href标记。它们会更改每个标记。我想单击它们全部-->/*[@id=“yui_3_10_0_1_1607785449630_1057”]无效选择例外:消息:给定xpath表达式”//*[@id^=“yui_3”]无效:语法错误:表达式不是合法表达式。我正在尝试收集链接,然后单击每个链接滚动,然后返回并单击下一个链接。@JeanS已使用正确的css选择器进行更新。@arundeep chohan我可以单击elems元素吗?当我使用elems[0]时。单击()我得到一个错误ElementNotInteractitableException:Message:Element似乎您得到了一个脚本标记,请尝试为标记或类似的东西添加标记类型。