硒找不到不在视野中的元素



我对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')

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

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

你也可以从这个链接这个链接获得帮助

相关内容

  • 没有找到相关文章

最新更新