简体   繁体   中英

URL must be a string selenium - selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'url' must be a string

I am making a script to automate the buying process of clothes by making requests but I get the error saying that url must be a string without specific error in the title ill put the code below im just not sure which url it is referincing but any help with this would really be appreciated so thank you - arthur

from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import ElementNotInteractableException
from selenium.webdriver.common.by import By
import random
import time
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
import requests
import webbrowser
from discord_webhooks import DiscordWebhooks
from datetime import datetime

class Profile:

  def __init__ (self, profileName, name, email, tel, address1, address2, zip, city, state, country, number, month, year, cvv):

      self.profileName = profileName
      self.name = name
      self.email = email
      self.tel = tel
      self.address1 = address1
      self.address2 = address2
      self.zip = zip
      self.city = city
      self.state = state
      self.country = country
      self.number = number
      self.month = month
      self.year = year
      self.cvv = cvv

class Item:

  def __init__ (self, product, color, size):

      self.product = product
      self.color = color
      self.size = size

chrome_options = Options()
chrome_options.add_argument("--headless")
profiles = [Profile]
webhook = DiscordWebhooks('empty for stack overflow')
headless = False

def lookingForItem():
  print("Monitoring")
  testItem = Item("Acrylic Visor 6-Panel", "Red", "N/A")
  monitor(testItem)
  productLink = findProduct(testItem)
  checkout(productLink, testItem)

def checkout(productLink, item):
  startTime = time.perf_counter()
  userProfile = Profile("test", "Test Name", "test@gmail.com", "1111111111", "1 Test St", "", "10001", "New York", "NY", "USA", "4111111111111111", "08", "2020", "111")
  driver.get(productLink)
  done = False
  delay = 0
  if item.size.lower() != 'n/a' and item.size.lower() != "any":
      if item.size.lower() == 'random':
          driver.execute_script("""var sizePicker = Math.floor(Math.random() * document.getElementById('s').length);
          document.getElementById('s').selectedIndex = sizePicker;""")
      else:
          driver.execute_script("""for (var i=0; i<document.getElementById('s').length; i++){{
                          if (document.getElementById('s').options[i].text == '{}'){{
                            document.getElementById('s').selectedIndex = i;
                          }}
                        }}""".format(item.size))
  while(not done):
      done = addToCart()
  done = False
  while(not done):
      done = goToCheckout()
  driver.execute_script("""document.getElementById('order_billing_name').value = '{}';
  document.getElementById('order_email').value = '{}';
  document.getElementById('order_tel').value = '{}';
  document.getElementById('bo').value = '{}';
  document.getElementById('oba3').value = '{}';
  document.getElementById('order_billing_zip').value = '{}';
  document.getElementById('order_billing_city').value = '{}';
  document.getElementById('order_billing_state').value = '{}';
  document.getElementById('order_billing_country').value = '{}';
  document.getElementById('rnsnckrn').value = '{}';
  document.getElementById('credit_card_month').value = '{}';
  document.getElementById('credit_card_year').value = '{}';
  document.getElementsByClassName('iCheck-helper')[1].click();""".format(
  userProfile.name, userProfile.email, userProfile.tel,
  userProfile.address1, userProfile.address2, userProfile.zip,
  userProfile.city, userProfile.state, userProfile.country,
  userProfile.number, userProfile.month, userProfile.year, userProfile.cvv))
  print("Delaying")
  time.sleep(delay/1000)
  driver.find_element_by_xpath('//*[@id="orcer"]').send_keys(userProfile.cvv)
  driver.find_element_by_xpath('//*[@id="cart-cc"]/fieldset/p[2]/label/div/ins').click()
  driver.find_element_by_xpath('//*[@id="pay"]/input').click()
  #webbrowser.open("https://www.google.com/recaptcha/api2/anchor?ar=1&k=6LeWwRkUAAAAAOBsau7KpuC9AV-6J8mhw4AjC3Xz&co=aHR0cHM6Ly93d3cuc3VwcmVtZW5ld3lvcmsuY29tOjQ0Mw..&hl=en&v=oqtdXEs9TE9ZUAIhXNz5JBt_&size=invisible&cb=h0o95sjo44ya")
  print("Processing")
  endTime = time.perf_counter()
  finalTime = round((endTime - startTime), 3)
  sendWebhook(productLink, item, finalTime)

def sendWebhook(productLink, item, finalTime):
  webhook.set_content(color=0xCC3333,title='cheebBot1.0')
  webhook.add_field(name='Status', value='**Payment failed!**')
  webhook.add_field(name='Date/Time', value=str(datetime.now()))
  webhook.add_field(name='Item', value='**'+ item.product + '**')
  webhook.add_field(name='Item Link', value=productLink)
  webhook.add_field(name='Color', value=item.color)
  webhook.add_field(name='Size', value=item.size)
  webhook.add_field(name='Checkout Time', value=str(finalTime) + ' s')
  webhook.send()

def findProduct(item):
  container = driver.find_element_by_xpath('//*[@id="container"]').find_elements_by_class_name('inner-article')
  for article in container:
      if article.find_element_by_class_name("product-name").find_element_by_class_name("name-link").text == item.product and article.find_element_by_class_name("product-style").find_element_by_class_name("name-link").text == item.color:
          return(article.find_element_by_class_name("product-name").find_element_by_class_name("name-link").get_attribute("href"))

def monitor(item):
  try:
      driver.find_elements_by_xpath('//div[contains(text(), "' + item.product + '")]')
  except NoSuchElementException:
      driver.refresh()
      monitor(item)
  return

def productListed(product):
  try:
      driver.find_element_by_xpath("//*[contains(text(), " + product + ")]")
  except NoSuchElementException:
      time.sleep(.5)
      driver.refresh()
      productListed(product)
  return

def addToCart():
  try:
      driver.find_element_by_xpath('//*[@id="add-remove-buttons"]/input').click()
  except ElementNotInteractableException:
      time.sleep(.01)
      #driver.refresh()
      return False
  print("Adding to cart")
  return True

def goToCheckout():
  try:
      driver.find_element_by_xpath('//*[@id="cart"]/a[2]').click()
  except ElementNotInteractableException:
      time.sleep(.01)
      return False
  print("Checking out")
  return True

if __name__ == "__main__":
  if headless:
      driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=chrome_options)
  else:
      driver = webdriver.Chrome(ChromeDriverManager().install())
  driver.get("https://www.supremenewyork.com/shop/shirts/ziq2otwjc/c9qzbgrlm")
  lookingForItem()

#class euProfile: 
#   def __init__(self, name, age):
      

It maybe in this code that have error:

def findProduct(item):
  container = driver.find_element_by_xpath('//*[@id="container"]').find_elements_by_class_name('inner-article')
  for article in container:
      if article.find_element_by_class_name("product-name").find_element_by_class_name("name-link").text == item.product and article.find_element_by_class_name("product-style").find_element_by_class_name("name-link").text == item.color:
          return(article.find_element_by_class_name("product-name").find_element_by_class_name("name-link").get_attribute("href"))

the code does not have a fallback when it did not satisfy the condition in for loop

from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import ElementNotInteractableException
from selenium.webdriver.common.by import By
import random
import time
from selenium.webdriver.chrome.options import Options
#from webdriver_manager.chrome import ChromeDriverManager
import requests
import webbrowser
from discord_webhooks import DiscordWebhooks
from datetime import datetime
from selenium import webdriver


class Profile:

    def __init__(self, profileName, name, email, tel, address1, address2, zip, city, state, country, number, month, year, cvv):
        self.profileName = profileName
        self.name = name
        self.email = email
        self.tel = tel
        self.address1 = address1
        self.address2 = address2
        self.zip = zip
        self.city = city
        self.state = state
        self.country = country
        self.number = number
        self.month = month
        self.year = year
        self.cvv = cvv


class Item:

    def __init__(self, product, color, size):
        self.product = product
        self.color = color
        self.size = size


class Scraper:
    def __init__(self, headless):
        chrome_options = Options()
        chrome_options.add_argument("--headless")
        profiles = [Profile]
        self.webhook = DiscordWebhooks('empty for stack overflow')

        if headless:
            self.driver = webdriver.Chrome(executable_path="chromedriver.exe", chrome_options=chrome_options)
        else:
            self.driver = webdriver.Chrome(executable_path="chromedriver.exe")

        self.driver.get("https://www.supremenewyork.com/shop/shirts/ziq2otwjc/c9qzbgrlm")


    def lookingForItem(self):
        print("Monitoring")
        testItem = Item("Acrylic Visor 6-Panel", "Red", "N/A")
        self.monitor(testItem)
        productLink = self.findProduct(testItem)
        print(productLink)
        self.checkout(productLink, testItem)


    def checkout(self, productLink, item):
        startTime = time.perf_counter()
        userProfile = Profile("test", "Test Name", "test@gmail.com", "1111111111", "1 Test St", "", "10001", "New York", "NY", "USA", "4111111111111111", "08",
                              "2020", "111")
        self.driver.get(productLink)
        done = False
        delay = 0
        if item.size.lower() != 'n/a' and item.size.lower() != "any":
            if item.size.lower() == 'random':
                self.driver.execute_script("""var sizePicker = Math.floor(Math.random() * document.getElementById('s').length);
              document.getElementById('s').selectedIndex = sizePicker;""")
            else:
                self.driver.execute_script("""for (var i=0; i<document.getElementById('s').length; i++){{
                              if (document.getElementById('s').options[i].text == '{}'){{
                                document.getElementById('s').selectedIndex = i;
                              }}
                            }}""".format(item.size))
        while (not done):
            done = self.addToCart()
        done = False
        while (not done):
            done = self.goToCheckout()
        self.driver.execute_script("""document.getElementById('order_billing_name').value = '{}';
          document.getElementById('order_email').value = '{}';
          document.getElementById('order_tel').value = '{}';
          document.getElementById('bo').value = '{}';
          document.getElementById('oba3').value = '{}';
          document.getElementById('order_billing_zip').value = '{}';
          document.getElementById('order_billing_city').value = '{}';
          document.getElementById('order_billing_state').value = '{}';
          document.getElementById('order_billing_country').value = '{}';
          document.getElementById('rnsnckrn').value = '{}';
          document.getElementById('credit_card_month').value = '{}';
          document.getElementById('credit_card_year').value = '{}';
          document.getElementsByClassName('iCheck-helper')[1].click();""".format(
            userProfile.name, userProfile.email, userProfile.tel,
            userProfile.address1, userProfile.address2, userProfile.zip,
            userProfile.city, userProfile.state, userProfile.country,
            userProfile.number, userProfile.month, userProfile.year, userProfile.cvv))
        print("Delaying")
        time.sleep(delay / 1000)
        self.driver.find_element_by_xpath('//*[@id="orcer"]').send_keys(userProfile.cvv)
        self.driver.find_element_by_xpath('//*[@id="cart-cc"]/fieldset/p[2]/label/div/ins').click()
        self.driver.find_element_by_xpath('//*[@id="pay"]/input').click()
        # webbrowser.open("https://www.google.com/recaptcha/api2/anchor?ar=1&k=6LeWwRkUAAAAAOBsau7KpuC9AV-6J8mhw4AjC3Xz&co=aHR0cHM6Ly93d3cuc3VwcmVtZW5ld3lvcmsuY29tOjQ0Mw..&hl=en&v=oqtdXEs9TE9ZUAIhXNz5JBt_&size=invisible&cb=h0o95sjo44ya")
        print("Processing")
        endTime = time.perf_counter()
        finalTime = round((endTime - startTime), 3)
        self.sendWebhook(productLink, item, finalTime)


    def sendWebhook(self,productLink, item, finalTime):
        self.webhook.set_content(color=0xCC3333, title='cheebBot1.0')
        self.webhook.add_field(name='Status', value='**Payment failed!**')
        self.webhook.add_field(name='Date/Time', value=str(datetime.now()))
        self.webhook.add_field(name='Item', value='**' + item.product + '**')
        self.webhook.add_field(name='Item Link', value=productLink)
        self.webhook.add_field(name='Color', value=item.color)
        self.webhook.add_field(name='Size', value=item.size)
        self.webhook.add_field(name='Checkout Time', value=str(finalTime) + ' s')
        self.webhook.send()


    def findProduct(self, item):
        container = self.driver.find_element_by_xpath('//*[@id="container"]').find_elements_by_class_name('inner-article')
        for article in container:
            if article.find_element_by_class_name("product-name").find_element_by_class_name(
                    "name-link").text == item.product and article.find_element_by_class_name("product-style").find_element_by_class_name(
                    "name-link").text == item.color:
                return (article.find_element_by_class_name("product-name").find_element_by_class_name("name-link").get_attribute("href"))

        print("THERE IS NOT ELEMENT FOUND FOR THE HREF YOU ARE LOOKING FOR SO THIS WILL RETURN NONE")


    def monitor(self, item):
        try:
            self.driver.find_elements_by_xpath('//div[contains(text(), "' + item.product + '")]')
        except NoSuchElementException:
            self.driver.refresh()
            self.monitor(item)
        return

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'using' must be a string using waits and expected conditions Python selenium for write review google maps (selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator ) selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'cookie' while adding cookies using selenium webdriver selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'ELEMENT' error when switching frame in Selenium Selenium By.CSS_SELECTOR selenium.common.exceptions.InvalidArgumentException: Message: invalid argument selenium.common.exceptions.InvalidArgumentException: Message: invalid argument error invoking get() using Selenium Webdriver through Python selenium.common.exceptions.InvalidArgumentException: Message: invalid argument while iterating through a list of urls and passing as argument to get() solved: python: selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator Selenium InvalidArgumentException: invalid argument: 'name' must be a string selenium.common.exceptions.InvalidArgumentException: Message: File not found while trying to upload image by url through selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM