【发布时间】:2019-12-16 09:02:42
【问题描述】:
我尝试获取的内容
<span class="noactive">0 Jours restants</span>
(这是保修的到期日期) 但我不知道如何获取(我需要将其打印到文件中)
我的代码
def scrapper_lenovo(file, line):
CHROME_PATH = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
CHROMEDRIVER_PATH = 'C:\webdriver\chromedriver'
WINDOW_SIZE = "1920,1080"
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)
chrome_options.binary_location = CHROME_PATH
d = driver.Chrome(executable_path=CHROMEDRIVER_PATH,
chrome_options=chrome_options)
d.get("https://pcsupport.lenovo.com/fr/fr/warrantylookup")
search_bar = d.find_element_by_xpath('//*[@id="input_sn"]')
search_bar.send_keys(line[19])
search_bar.send_keys(Keys.RETURN)
time.sleep(4)
try:
warrant = d.find_element_by_xpath('//*[@id="W-Warranties"]/section/div/div/div[1]/div[1]/div[1]/p[1]/span')
file.write(warrant)
except:
print ("test")
pass
if ("In Warranty" not in d.page_source):
file.write(line[3])
file.write("\n")
d.close()
如您所见,我尝试打印“保证”的内容,但我找不到任何允许它的功能(我看到一些使用 .text()、.gettext() 但无论出于何种原因我都无法获得他们工作)。
【问题讨论】:
-
你试过
warrant.getAttribute(“innerHTML”)吗? -
只使用warrant.text
-
我尝试了这个页面中的内容,但我无法让它工作,也许我错过了包含或错误地使用了 text/gettext
标签: python-3.x selenium web