python - 如何在selenium上抓取产品详细信息页面

标签 python selenium selenium-webdriver selenium-chromedriver selenium-rc

我正在学习 Selenium 。现在我的这段代码可以从这个url https://www.daraz.com.bd/consumer-electronics/?spm=a2a0e.pdp.breadcrumb.1.4d20110bzkC0bn 的字体页面中抓取所有产品标题。但我想单击此页面的每个产品链接,这将带我进入产品详细信息页面,以便我可以从产品详细信息页面抓取信息。这是我的代码:

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
from selenium.common.exceptions import TimeoutException

#argument for incognito Chrome
option = webdriver.ChromeOptions()
option.add_argument(" — incognito")

browser = webdriver.Chrome()

browser.get("https://www.daraz.com.bd/consumer-electronics/?spm=a2a0e.pdp.breadcrumb.1.4d20110bzkC0bn")

# Wait 20 seconds for page to load
timeout = 20
try:
    WebDriverWait(browser, timeout).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='c16H9d']")))
except TimeoutException:
    print("Timed out waiting for page to load")
    browser.quit()



# find_elements_by_xpath returns an array of selenium objects.
titles_element = browser.find_elements_by_xpath("//div[@class='c16H9d']")


# use list comprehension to get the actual repo titles and not the selenium objects.
titles = [x.text for x in titles_element]
# print out all the titles.
print('titles:')
print(titles, '\n')
browser.quit()

最佳答案

我建议您获取href并一一打开,而不是您所说的点击。

您需要此定位器:By.XPATH, "//div[@class='c16H9d']//a",并使用 .visibility_of_all_elements_ located 等待所有元素而不是 .visibility_of_element_ located

之后,使用以下方法获取href:.get_attribute('href')

并打开一个新窗口,其中包含已获取的特定 href

browser.get("https://www.daraz.com.bd/consumer-electronics/?spm=a2a0e.pdp.breadcrumb.1.4d20110bzkC0bn")

# Wait 20 seconds for page to load
timeout = 20

elements = WebDriverWait(browser, timeout).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='c16H9d']//a")))

for element in elements:
    #get href
    href = element.get_attribute('href')
    print(href)
    #open new window with specific href
    browser.execute_script("window.open('" +href +"');")
    # switch to new window
    browser.switch_to.window(browser.window_handles[1])


    #......now you are on the new window, scrape here
    #example to scrape 'title' in the new window
    xx = WebDriverWait(browser, timeout).until(EC.visibility_of_element_located((By.CLASS_NAME, "pdp-mod-product-badge-title")))
    print(xx.text)


    #close the new window
    browser.close()
    #back to main window
    browser.switch_to.window(browser.window_handles[0])

browser.quit()

关于python - 如何在selenium上抓取产品详细信息页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60730509/

相关文章:

python - 在 Python 中删除前导/结尾和内部多个空格但不删除制表符、换行符或返回字符

python - 为经过训练的 tensorflow 网络中的所有输入获取相同的预测值

python - 使用 OpenCV 3 和 python 3 按区域对图像轮廓进行排序的最佳解决方案

node.js - 如何检查元素是否不存在 selenium nodejs

selenium - 如何获取 Selenium 网格利用率的统计数据

python - 在 python 上使用 cloudflare 保护连接到 websocket

Python selenium iselementpresent 给出 "wrong"答案

python - 如何使用 Kameleo 和 Selenium 加载多个 chrome 浏览器?

java - Selenium - 单击标签(它会暂时改变颜色),然后标签恢复为未单击状态

java - 使用 Appium 处理 Android 应用中的弹出窗口