Selenium脚本不适用于某些Chrome配置文件,但适用于其他

时间:2020-10-29 16:56:28

标签: python selenium-webdriver google-chrome-extension selenium-chromedriver chrome-profile

这是无法在我的代码中使用的此类机器人:

def __init__(self, player, nation, league, quality, rarity, snipe_price, sell_price, email, password):
    chrome_options = Options()
    chrome_options.add_argument(f'user-data-dir={self.get_chrome_settings_path()}')
    self.driver = webdriver.Chrome(options = chrome_options)
    self.driver.get('https://www.ea.com/fifa/ultimate-team/web-app/')
    self.player = player
    self.snipe_price = snipe_price
    self.sell_price = sell_price
    self.email = email
    self.password = password
    self.quality = quality
    self.rarity = rarity
    self.nation = nation
    self.league = league
    self.snipe_state = True


def login(self):
    #self.driver.quit()
    logged_in = False

    try:
        login_button = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/main/div/div/div/button[1]'))).click()
    except:
        logged_in = True

    
    if not(logged_in):
        try:
            email_adress_input = WebDriverWait(self.driver, 5).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/form/div[3]/div[1]/div[1]/div/ul[1]/li[1]/div/span[1]/span/input')))
            if len(email_adress_input.get_attribute('value')) == 0:
                email_adress_input.send_keys(self.email.strip())
            password_input = self.driver.find_element_by_xpath('/html/body/div[2]/form/div[3]/div[1]/div[1]/div/ul[1]/li[2]/div/span[1]/span/input').send_keys(self.password)
            login = self.driver.find_element_by_xpath('/html/body/div[2]/form/div[3]/div[1]/div[2]/a[1]').click()
        except:
            pass


def snipe(self):
    transfers = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/main/section/nav/button[3]'))).click()
    transfer_market = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/main/section/section/div[2]/div/div/div[2]/div[2]'))).click()
    buy_price_input = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[2]/div[6]/div[2]/input'))).send_keys(self.snipe_price)

    counter = 0
    successive_attempts = randint(1, 50)
    sleep_time = randint(1, 30)
    increase = True

    while self.snipe_state:
        counter += 1

        try:
            self.driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div/div[2]/div[2]').click()
        except:
            pass

        self.apply_filters()



        if len(self.driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[2]/div[2]/div[2]/input').get_attribute('value')) == 0:
            increase = True
        
        elif int(self.driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[2]/div[2]/div[2]/input').get_attribute('value')) > 700:
            increase = False

        if increase:
            self.driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[2]/div[2]/div[2]/button[2]').click()
        else:
            self.driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[2]/div[2]/div[2]/button[1]').click()





        if int(self.driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[2]/div[6]/div[2]/input').get_attribute('value').replace(',', '')) == int(self.snipe_price):
            try:
                self.driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div[2]/div/div[2]/button[2]').click()
            except:
                pass
        else:
            buy_price_input = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[2]/div[6]/div[2]/input'))).send_keys(self.snipe_price)

        

        try:
            WebDriverWait(self.driver, 0.6).until(EC.element_to_be_clickable((By.XPATH, '/html/body/main/section/section/div[2]/div/div/section[2]/div/div/div[2]/div[2]/button[2]'))).click()
            self.driver.find_element_by_xpath('/html/body/div[4]/section/div/div/button[1]').click()
            #WebDriverWait(self.driver, 1).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[4]/section/div/div/button[1]'))).click()
            WebDriverWait(self.driver, 1).until(EC.element_to_be_clickable((By.XPATH, '/html/body/main/section/section/div[2]/div/div/section[2]/div/div/div[2]/div[2]/div[1]/button'))).click()
            self.sell_player()

            #WebDriverWait(self.driver, 3).until(EC.element_to_be_clickable((By.XPATH, '/html/body/main/section/section/div[2]/div/div/section[2]/div/div/div[2]/div[3]/button[8]'))).click()
        except:
            #/html/body/main/section/section/div[2]/div/div/section[2]/div/div/div[2]/div[3]/button[8]
            pass
            
        try:
            WebDriverWait(self.driver, 1).until(EC.element_to_be_clickable((By.XPATH, '/html/body/main/section/section/div[1]/button[1]'))).click()
        except Exception as e:
            print(e)


        if counter == successive_attempts:
            self.driver.find_element_by_xpath('/html/body/main/section/section/div[1]/button[1]').click()
            sleep(sleep_time)
            counter = 0
            successive_attempts = randint(1, 100)
            sleep_time = randint(1, 30)


    self.driver.quit()




def sell_player(self):
    
    sleep(2)
    start_price_input = self.driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div/section[2]/div/div/div[2]/div[2]/div[2]/div[2]/div[2]/input')
    start_price_input.click()
    start_price_input.send_keys('0')
    sleep(1)
    start_price = int(self.sell_price) - 100
    start_price_input.send_keys(str(start_price))
    max_price_input = self.driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div/section[2]/div/div/div[2]/div[2]/div[2]/div[3]/div[2]/input')
    max_price_input.click()
    max_price_input.send_keys('0')
    sleep(1)
    max_price_input.send_keys(str(self.sell_price))
    sleep(1)
    execute = self.driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div/section[2]/div/div/div[2]/div[2]/div[2]/button').click()


def get_chrome_settings_path(self):

    start = r'C:\Users'
    target = ['AppData', 'Local', 'Google', 'Chrome', 'User Data', 'Default']
    chrome_settings_path = ''

    for path,_,_ in os.walk(start):
        path = str(path).split('\\')

        try:
            if path[3:] == target:
                chrome_settings_path = '\\'.join(path)
                break
        except:
            pass

    print(chrome_settings_path)

    return chrome_settings_path



def apply_filters(self):
    if len(self.player) == 0:

    
        if self.nation.lower() != 'nationality' and len(self.nation) > 0:

            try:
                nation_option = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[1]/div[6]/div/div'))).click()

            except:
                pass

            run = True
            i = 2

            while run:
                try: 
                    nation_option = self.driver.find_element_by_xpath(f'/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[1]/div[6]/div/ul/li[{i}]')

                    if nation_option.text.lower() == self.nation.lower():
                        nation_option.click()
                        run = False


                except:
                    run = False


                i += 1



        if self.league.lower() != 'league' and len(self.league) > 0:

            try:
                league_option = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[1]/div[7]/div/div'))).click()
            except:
                pass

            run = True
            i = 2


            while run:
                try:
                    league_option = self.driver.find_element_by_xpath(f'/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[1]/div[7]/div/ul/li[{i}]')
                    print(league_option.text.lower())

                    if league_option.text.lower() == self.league.lower():
                        league_option.click()
                        run = False


                except:
                    run = False

                i += 1


    else:

        if self.player.lower() not in self.driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[1]/div[1]/div/div[1]/input').get_attribute('value').lower():
            player_input = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[1]/div[1]/div/div[1]/input'))).send_keys(self.player)
            sleep(1)
            player_select = self.driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[1]/div[1]/div/div[2]/ul/button[1]/span[1]').click()



    if self.quality.lower() != 'quality' and len(self.quality) > 0:

        try:
            quality_filter = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[1]/div[2]/div/div'))).click()

        except:
            pass

        for i in range(2, 6):

            try:
                quality_option = self.driver.find_element_by_xpath(f'/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[1]/div[2]/div/ul/li[{i}]')

                if quality_option.text.lower() == self.quality.lower():
                    quality_option.click()
                    break
            except:
                pass


    if self.rarity.lower() != 'rarity' and len(self.rarity) > 0:

        try:
            rarity_option = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[1]/div[3]/div/div'))).click()
        except:
            pass

        rarities = []

        for i in range(2, 13):

但是当我将get_chrome_settings_path更改为:

    def get_chrome_settings_path(self):

    start = r'C:\Users'
    target = ['AppData', 'Local', 'Google', 'Chrome', 'User Data', 'Profile 1']
    chrome_settings_path = ''

    for path,_,_ in os.walk(start):
        path = str(path).split('\\')

        try:
            if path[3:] == target:
                chrome_settings_path = '\\'.join(path)
                break
        except:
            pass

    print(chrome_settings_path)

    return chrome_settings_path

它之所以有效,是因为我使用配置文件1用户而不是Default chrome用户,但是我需要使其与默认用户一起使用,以便使代码在我的另一台计算机上正常工作。

对此我感到非常困惑。有人可以帮忙吗?

0 个答案:

没有答案
相关问题