简单如果语句不起作用比较字符串

时间:2017-10-10 21:37:52

标签: python string python-3.x if-statement

我只是想尝试导航到网页并检查可用性。当我找到可用性状态并尝试查看它是否为" In Stock。"然后我想执行一些动作(在示例print" 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")

2 个答案:

答案 0 :(得分:0)

您可以在添加任何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.

答案 1 :(得分:-1)

我没有Safari,所以我使用了Chrome。

protected override void ValidateFields(List<IFieldValidationResult> validationResults)
        {
            if (Peso!=null && !Peso.Peso_Caliente.HasValue)
                validationResults.Add(FieldValidationResult.CreateErrorWithTag(Peso_CalienteProperty,"No se ha capturado el peso", "Captura_PesoCalienteCanExecute"));
            if (Peso!=null && !Peso.IC.HasValue)
                validationResults.Add(FieldValidationResult.CreateError(LabelConformidadValidadionProperty, "No se ha capturado el indicador IC"));
        }

出于某种原因,这给出了期望的结果。一定要给出一些条件来摆脱while循环。例如,

driver = webdriver.Chrome()