[英]Simple If Statement Not Working Comparing Strings
我只是在嘗試導航到網頁並檢查可用性。 當我找到可用性狀態並嘗試查看它是否為“有貨”時。 然后我想執行一些操作(在示例打印“ Found”中)。 當我測試它時,變量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")
您可以在添加任何if-else之前使用print
檢查自己,檢查字符串,然后根據該字符串添加條件:
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.
我沒有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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.