我有以下代码:
def get_country(product_number):
options = Options();
options.add_argument("--headless")
chrome_driver_path = "D://Softwares//ChromeDriver//chromedriver.exe"
driver = webdriver.Chrome(chrome_driver_path,chrome_options=options);
driver.get("www.example.com")
srch = driver.find_element_by_xpath("//input[@placeholder='Search millions of products and datasheets']")
srch.send_keys(product_number)
btn = driver.find_element_by_xpath("//button[@class='Embedded-search-button']")
btn.click()
tit = []
man = []
des = []
try:
parent=driver.find_element_by_css_selector("dl.BuyingOptions-labeledValues")
country=parent.find_element_by_xpath("//dt[contains(.,'Country Of Origin')]/following::dd[1]")
co = country.text
except NoSuchElementException:
co = ""
pare = driver.find_elements_by_xpath("//div[@class='WideSidebarProductList-item-details']")
if (len(pare)!=0):
for par in pare:
title = par.find_element_by_xpath("./h4[@class='WideSidebarProductList-item-title']")
manu = par.find_element_by_xpath("./h5[@class='WideSidebarProductList-item-manufacturer']")
desc = par.find_element_by_xpath("./p[@class='WideSidebarProductList-item-description']")
tit.append(title.text)
man.append(manu.text)
des.append(desc.text)
print("Done")
return co,tit,man,des
现在,当我不使用chrome选项时,代码可以正常工作,但是当我使用无头选项时,chrome返回tit
,man
和des
的空列表。日志如下:
DevTools listening on ws://127.0.0.1:51306/devtools/browser/4b0749ed-d971-44f4-9a29-8e0975f24432
[0801/152120.248:INFO:CONSOLE(1)] "Download the Apollo DevTools for a better development experience:
https://chrome.google.com/webstore/detail/apollo-client-developer-t/jdkknkkbebbapilgoeccciglkfbmbnfm", source: https://static4.arrow.com/dist/arrow/browser/vendor.31e3ee1d4101f4c0fe7f.js (1)
[0801/152122.459:INFO:CONSOLE(5)] "Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://cdn.krxd.net') does not match the recipient window's origin ('https://www.arrow.com').", source: https://cdn.krxd.net/ctjs/controltag.js.05f9d0dad02f8a1b0b028b868bc3a3e2 (5)
[0801/152130.672:INFO:CONSOLE(1)] "global-jssState", source: https://www.arrow.com/dist/arrow/browser/11.45371a769e02a2e956ed.js (1)
[0801/152130.674:INFO:CONSOLE(1)] "pdp-wrapper-this", source: https://www.arrow.com/dist/arrow/browser/11.45371a769e02a2e956ed.js (1)
[0801/152140.790:INFO:CONSOLE(1)] "Download the Apollo DevTools for a better development experience: https://chrome.google.com/webstore/detail/apollo-client-developer-t/jdkknkkbebbapilgoeccciglkfbmbnfm", source: https://static4.arrow.com/dist/arrow/browser/vendor.31e3ee1d4101f4c0fe7f.js (1)
[0801/152141.400:INFO:CONSOLE(1)] "global-jssState", source: https://www.arrow.com/dist/arrow/browser/11.45371a769e02a2e956ed.js (1)
[0801/152141.402:INFO:CONSOLE(1)] "pdp-wrapper-this", source: https://www.arrow.com/dist/arrow/browser/11.45371a769e02a2e956ed.js (1)
Done
['', '', '', ''] ['', '', '', ''] ['', '', '', '']
我是否需要日志建议的apollo客户端开发人员,或者是否可以解决此问题?
答案 0 :(得分:1)
我遇到了类似的问题。 您可以尝试将 WebDriverWait 与 lambda 结合使用,其中您指定查找带有文本的元素,否则可能会加载网页,但不会加载元素文本。
一个例子是:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 10 stands for time to definitely wait, it will look every 500 milliseconds if element returns successful
btn = WebDriverWait(driver, 10).until(
lambda x: x.find_element_by_xpath('/your/xpath'))
btn.click()
title = WebDriverWait(driver, 10).until(
lambda x: x.find_element_by_xpath('/your/xpath').text)
tit.append(title)
return tit