[英]Get JavaScript function call value using Selenium
通过链接文本找到“尺寸图”链接,单击它并提取数据,例如:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get('http://www.jabong.com/athena-Red-Black-Top-476472.html?pos=3')
wait = WebDriverWait(driver, 10)
chart = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "size chart")))
chart.click()
for title in driver.find_elements_by_css_selector("div.size-chart-body div.size-chart table th"):
print title.text
driver.close()
打印(为示例起见,表标题行):
Indian Size
Euro Size
Garment Bust (In.)
Garment Waist (in.)
Garment Hip (in.):
请注意,您不需要硒来获取尺寸表数据,它已经在DOM中,但是在您单击“尺寸表”之前是不可见的。 您可以使用Scrapy达到相同尺寸的图表表。 来自“ Scrapy Shell”的演示:
$ scrapy shell http://www.jabong.com/athena-Red-Black-Top-476472.html?pos=3
In [1]: for title in response.css("div.size-chart-body div.size-chart table th")[1:]:
print title.xpath("text()").extract()[0]
...:
Indian Size
Euro Size
Garment Bust (In.)
Garment Waist (in.)
Garment Hip (in.)
对于Koovs,您仍然可以避免使用硒,而是通过手动提取类别和交易名称来构造大小图表URL,例如:
$ scrapy shell http://www.koovs.com/only-onlall-stripe-ls-shirt-59554.html?from=category-651
In [1]: category = response.xpath("//input[@id='master_category_name_id_ref']/@value").extract()[0]
In [2]: deal = response.xpath("//input[@id='deal_id']/@value").extract()[0]
In [3]: "http://www.koovs.com/koovs/sizechart/women/{category}/{deal}".format(category=category, deal=deal)
Out[3]: 'http://www.koovs.com/koovs/sizechart/women/Shirts--651--799--896/59554'
而且,如果您仍然想使用硒,这里是:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get('http://www.koovs.com/only-onlall-stripe-ls-shirt-59554.html?from=category-651&skuid=236376')
wait = WebDriverWait(driver, 10)
chart = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "a[size_chart]")))
chart.click()
driver.switch_to.window(driver.window_handles[-1])
print driver.current_url
driver.close()
打印:
http://www.koovs.com/koovs/sizechart/women/Shirts--651--799--896/59554
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.