2017-10-10 114 views
0

我只是簡單地嘗試導航到網頁並檢查可用性。當我找到可用狀態並嘗試查看它是否爲「有貨」時。那麼我想執行一些操作(在示例中打印「找到」)。當我測試它時,變量InStockCheck似乎沒有註冊爲字符串。我相信當我用簡單如果語句不起作用比較字符串

InStockCheck = driver.find_element_by_id("availability").text 

它不是一個字符串?

電流輸出是:

InStock. 
Yellow 

所需的輸出:

InStock. 
Found 

代碼:

from selenium import webdriver 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.by import By 
from selenium.webdriver.common.keys import Keys 
from selenium.common.exceptions import NoSuchElementException 
from selenium.common.exceptions import TimeoutException 

import bs4 as bs 

FoundItem = "Nope" 
driver = webdriver.Safari() 

while (FoundItem == "Nope"): 

    #driver = webdriver.Safari() 
    driver.get("https://www.amazon.ca/gp/product/B01MUAGZ49/ref=s9_acsd_top_hd_bw_bHp5rsB_c_x_w?pf_rd_m=A3DWYIK6Y9EEQB&pf_rd_s=merchandised-search-3&pf_rd_r=34J7S43PK58HEWFWQRCY&pf_rd_t=101&pf_rd_p=1a0d15fb-8f11-58d0-9960-246ad05b4dc8&pf_rd_i=16329250011") 

    #SourceCodeTest = driver.page_source 

    #Soup = bs.BeautifulSoup(SourceCodeTest, "lxml") 

    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "availability"))) 
    InStockCheck = driver.find_element_by_id("availability").text 
    InStockCheck = InStockCheck.replace(" ","") 
    print(InStockCheck) 

    if InStockCheck == "InStock.": 
     print("Found") 
    else: 
     print("Yellow") 

print("Pink") 
+6

您是否檢查過空白或其他不可見字符? – pvg

+4

是的,你可以使用'InStockCheck = InStockCheck.strip()'它打印時似乎有一個換行符。 –

+0

@pvg我不知道如何「檢查不可見字符」。 Jean所說的解決方案有效,但對於我未來的調試,我將如何檢查隱形字符? – Aiden

回答

-1

我沒有Safari瀏覽器,所以我用Chrome瀏覽器。

driver = webdriver.Chrome() 

由於某種原因,這給出了所需的結果。一定要給出一些條件來打破while循環。例如,

# other imports 
import time 

# other code 
numberOfRetries = 0 

while (FoundItem == "Nope"): 
    # other code 

    if InStockCheck == "InStock.": 
     print("Found") 
     break 
    elif numberOfTries > 4: 
     print("Not found after waiting for 5 seconds") 
     break 
    else: 
     print("Yellow") 
     numberOfRetries = numberOfRetries + 1 
     time.sleep(1) 

print("Pink") 
0

你可以只檢查自己使用print添加任何的if-else之前,檢查字符串,然後添加條件時,根據該字符串:

from selenium import webdriver 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.by import By 
from selenium.webdriver.common.keys import Keys 
from selenium.common.exceptions import NoSuchElementException 
from selenium.common.exceptions import TimeoutException 

import bs4 as bs 
import sys 
FoundItem = "Nope" 
driver = webdriver.Chrome() 

while (FoundItem == "Nope"): 

    #driver = webdriver.Safari() 
    driver.get("https://www.amazon.ca/gp/product/B01MUAGZ49/ref=s9_acsd_top_hd_bw_bHp5rsB_c_x_w?pf_rd_m=A3DWYIK6Y9EEQB&pf_rd_s=merchandised-search-3&pf_rd_r=34J7S43PK58HEWFWQRCY&pf_rd_t=101&pf_rd_p=1a0d15fb-8f11-58d0-9960-246ad05b4dc8&pf_rd_i=16329250011") 

    #SourceCodeTest = driver.page_source 

    #Soup = bs.BeautifulSoup(SourceCodeTest, "lxml") 

    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "availability"))) 
    InStockCheck = driver.find_element_by_id("availability").text 
    InStockCheck_ori = InStockCheck.strip() 
    print(InStockCheck_ori) 
    InStockCheck1 = InStockCheck.replace(" ","") 
    print(InStockCheck1) 

輸出:

In Stock. 
InStock.