【问题标题】:Selenium cannot find elements that are not immediately in viewSelenium 无法找到未立即显示的元素
【发布时间】:2020-11-11 13:41:48
【问题描述】:

我遇到了 Selenium 的问题。下面有问题的代码正在运行,可以记录我需要的所有内容,但它只能记录到 10 - 巧合的是,10 是我在页面上看到的产品数量而不向下滚动,所以我怀疑它不能点击这些,因为它们'从技术上讲,它不在视图中,并且需要滚动操作或使该元素可见的东西。我尝试了几种方法来做到这一点,但我无法找到一种优雅的最新方法。

大多数页面有大约 25 个项目,代码可以识别,但同样无法找到要单击的元素并会抛出此问题

On Item:10 of 25
list index out of range
On Item:11 of 25
list index out of range
On Item:12 of 25
list index out of range
On Item:13 of 25
list index out of range
On Item:14 of 25

....

class InspectProducts:

def __init__(self):
    phantom_js_path = '/usr/bin/phantomjs'
    self.driver = webdriver.PhantomJS(executable_path=phantom_js_path)
    self.driver.maximize_window()

 def test(self, url_path: str, category: str):
        self.driver.get(url_path)
        products = self.driver.find_elements_by_class_name('catProd-image-wrapper')
        for x in range(0, len(products)):

            try:
                print(f'On Item:{x} of {len(products)}')
                option = self.driver.find_elements_by_class_name('catProd-image-wrapper')[x]
                option.click()
                # on product page now
                product_name = self.driver.find_element_by_class_name('prod-title').text
                label = self.driver.find_elements_by_tag_name('img')
                product_image_label = label[4].get_attribute('src')
           
                if product_image_label.endswith('uploadB.jpg') or product_image_label.endswith('uploadA.jpg'):
                    nutrition_label = product_image_label
                    print(nutrition_label)
                elif not product_image_label.endswith('uploadB.jpg') or not product_image_label.endswith('uploadA.jpg'):
                    nutrition_label = 'no label'

                product_description = self.driver.find_element_by_class_name('productSectionContent').text
                product_rating = self.driver.find_elements_by_xpath('//*[@id="content"]/div[2]/form/div[1]/div/div[1]/div[3]/div[2]/div/span/span/span[1]')[0].text
                product_price = self.driver.find_elements_by_xpath('/html/body/div[3]/div[3]/div/div[2]/form/div/div/div[2]/div[2]/div[1]/span[2]/span')[0].text
                # you can print stuff here ..

                # back to product listing page
                self.utils.go_back_to_previous_page()
            except IndexError as e:
                print(e)
            print(f'ALL DONE {category}')


if __name__ == "__main__":
    inspect_products = InspectProducts()
    inspect_products.test('https://www.svncanada.com/list-amino-acid-blends.php', 'amino acids')

【问题讨论】:

    标签: python-3.x selenium phantomjs


    【解决方案1】:

    此代码是在 java 中,但这将帮助您首先加载所有项目。滚动到页面末尾,然后开始收集数据。

    driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.END);
    

    您也可以从此链接This Link获得帮助

    【讨论】:

    • 试过那个^的翻译,没用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    • 2022-01-04
    • 1970-01-01
    • 2018-03-31
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多