2017-10-10 71 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.