【问题标题】:Using selenium and python, i want to find if the website has a specific string in and if it does print yes or if it doesnt print no使用 selenium 和 python,我想查找网站是否有特定的字符串,如果它打印是或者它是否不打印
【发布时间】:2021-10-12 07:57:37
【问题描述】:

我目前正在访问一个网站并使用网站搜索栏搜索查询列表,然后该网站返回该查询是否可用或不可用字符串。我希望能够搜索查询,然后检查查询是否可用。

    try:
    nres = driver.find_elements_by_id(str("nres"))
    if nres ==
    print("Tag is unavailable")
except NoSuchElementException:
    print("Tag is available")

【问题讨论】:

  • 您能否通过提供一个运行代码进行搜索的真实网站示例来详细说明,期望的结果是什么以及您的代码的实际结果是什么?
  • @Fiuity 您能否分享您想要执行这些任务的网站的 URL 或提供相关的 HTML 以及您遇到问题的详细信息?
  • 所以网站是gamertagavailability.com,我基本上希望能够自动化整个过程,用列表中的玩家标签填充搜索栏,然后查看它们是否可用

标签: python string selenium web search


【解决方案1】:

此代码可能会对您有所帮助。

from selenium import webdriver
import time

driver = webdriver.Chrome(executable_path="path")
driver.implicitly_wait(6)
driver.maximize_window()
driver.get("https://www.gamertagavailability.com/check.php")
driver.find_element_by_id("mytag").send_keys("Sampletext")
driver.find_element_by_id("doitnow").click()
time.sleep(2)
check = driver.find_element_by_xpath("//div[@id='nres']/p[2]").text
if check == "is not available!":
    print("Tag is not available")
else:
    print("Tag is available")
time.sleep(2)
driver.quit()

【讨论】:

    【解决方案2】:

    您可以在下面查看它,我使用explicitWait在输入中输入值并单击按钮。

    driver.get("https://www.gamertagavailability.com/")
    
    gamerTags=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,"mytag")))
    gamerTags.send_keys("GameZoneOwner")
    
    checkNowBtn =WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"doitnow")))
    checkNowBtn.click()
    
    #I have taken the common XPath for the status as it's ID is different and verified for both the status.
    status = driver.find_element_by_xpath("//div[@id='test']/div//p[2]").text
    
    if status == "seems to be available!":
       print("The tag is available ")
    elif status == "is not available!":
       print("Tag is not available")
    

    导入

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多