Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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/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
Selenium:选择并通过direct迭代<;李>;a<;ul>;家长使用python?_Python_Selenium - Fatal编程技术网

Selenium:选择并通过direct迭代<;李>;a<;ul>;家长使用python?

Selenium:选择并通过direct迭代<;李>;a<;ul>;家长使用python?,python,selenium,Python,Selenium,这是网站的一部分,我想刮 这是我的python selenium代码: className = "_29hl" try: search = WebDriverWait(driver, 10).until( EC.presence_of_all_elements_located((By.CLASS_NAME, className,)) ) print( search ) print( search[0] )

这是网站的一部分,我想刮

这是我的python selenium代码:

className = "_29hl"
try:
    search = WebDriverWait(driver, 10).until(
        EC.presence_of_all_elements_located((By.CLASS_NAME, className,))
    )

    print( search )      
    print( search[0] )   
    # the two print statement above print two different webElement objects
    # why is that?

    # I want to do something like this...
    lists = search.direct_children
    links = [ each.find_element_by_tag_name('a') for each in lists ]

    for link in links:
        link.click()
        # other codes...

finally:
    driver.quit()
当我从控制台选择
className
时,它返回一个集合

因此,我选择第一个元素,它返回我想要刮取的
及其
  • 子元素

    如何使用python selenium选择和迭代
  • 元素?
    我的目标是选择每个
  • 中的
    ,然后单击该所有元素的存在位置,查找并返回具有指定类的所有元素,因此搜索是一个包含具有指定类的所有元素的列表。调用search[0]时,它将从列表中获取第一个元素

    要获取此元素的直接子元素,请使用下面的定位器

    lists = search[0].find_elements_by_xpath("./li")
    

    您可以使用xpath。表示当前元素,即ul,然后“/”表示直接子元素

    完整代码

    className = "_29hl"
    try:
        search = WebDriverWait(driver, 10).until(
            EC.presence_of_all_elements_located((By.CLASS_NAME, className,))
        )
    
        print( search )      
        print( search[0] )   
        # the two print statement above print two different webElement objects
        # why is that?
    
        # I want to do something like this...
        lists = search[0].find_elements_by_xpath("./li")
    
        links = [ each.find_elements_by_tag_name('a') for each in lists ]
    
        for link in links:
            link.click()
            # other codes...
    
    finally:
        driver.quit()
    

    这有什么问题,代码看起来不错?没有selenium命令,或者我还不知道,它获取webElement对象的直接子对象。另外,当我运行这个程序并打印
    search
    search[0]
    时,它们是两个不同的webElement对象。我想要的是选择准确的
    。并迭代它的子项…但我不知道
    列表如何不可迭代…我们如何迭代it@EricEchemaneupdatedi使用的是find元素,它只返回符合条件的第一个元素,我将它们更改为elementsthank you…但是我如何单击它?例如,…列表[0]。单击()@Eric您已经有for循环来查找标记并单击它,只需替换代码中的列表即可
    
    className = "_29hl"
    try:
        search = WebDriverWait(driver, 10).until(
            EC.presence_of_all_elements_located((By.CLASS_NAME, className,))
        )
    
        print( search )      
        print( search[0] )   
        # the two print statement above print two different webElement objects
        # why is that?
    
        # I want to do something like this...
        lists = search[0].find_elements_by_xpath("./li")
    
        links = [ each.find_elements_by_tag_name('a') for each in lists ]
    
        for link in links:
            link.click()
            # other codes...
    
    finally:
        driver.quit()