创建登录和注销循环Python Selenium [英] Create Login and Logout Loop Python Selenium

查看:201
本文介绍了创建登录和注销循环Python Selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个已有的登录和注销循环

Hi I'm trying to Create a login and logout loop that I've had

访问www.shopee.com.my

to www.shopee.com.my

from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import csv
import urllib.parse

url = 'https://shopee.com.my/'

driver = webdriver.Chrome('E:/users/Asashin/Desktop/Bots/others/chromedriver.exe')
driver.get(url)

actions = ActionChains(driver)
wait = WebDriverWait(driver, 10)
time.sleep(1)

lan=driver.find_element_by_xpath('//button[text()="English"]')
if lan.is_displayed:
    lan.click()
    time.sleep(0.5)

ads=driver.find_element_by_class_name('shopee-popup__close-btn')
if ads.is_displayed:
    ads.click()
    time.sleep(0.5)

time.sleep(2)


firstLevelMenu = driver.find_element_by_xpath('//ul[@class="navbar__links"]')
actions.move_to_element(firstLevelMenu).perform();

secondLevelMenu = driver.find_element_by_xpath('//li[contains(text(), "Login")]');
secondLevelMenu.click();

time.sleep(3)
f= open("account.csv","r")

for line in f :
      fields = line.split(",")

      a = fields[0]
      b = fields[1]
      c = fields[2]

      driver.find_element_by_xpath("//input[@type='text']").send_keys(str(a))
      driver.find_element_by_xpath("//input[@type='password']").send_keys(str(c))
      time.sleep(0.8)

      button = driver.find_element_by_xpath('//button[@class="_1BMmPI _37G57D _7h_6kj _1qIIqG _3JP5il"]')
      if button.is_displayed:
          button.click()
          time.sleep(2)
      else:
          continue

      time.sleep(1)

      ads2=driver.find_element_by_class_name('shopee-popup__close-btn')


      if ads2.is_displayed:
          ads2.click()
          time.sleep(0.5)
      try:
          banned = driver.switch_to_alert.text("Your account has been banned")
          banned2 = driver.switch_to_alert.text("Invalid Email")

          if banned.is_displayed():
            while True:
                  q= open ('Banned Accounts.txt',w)
                  File_object.write(a)

                  driver.get_element_by_class_name('input-with-status__input').clear()
                  driver.find_element_by_xpath("//input[@type='password']").clear()
          continue
      except:
          browser.driver.close();

这是帐户名和密码:

here is the account names and password:

问题是我尝试了悬停方法,但似乎不起作用. div类由悬停方法组成,并且下拉菜单仅与注销菜单一起出现. 怎么解决?

The thing is that I've tried the hover method it doesn't seem to work. The div class is made of hovering method and the drop-down menu will only happen together with the logout menu. How can it be solved?

推荐答案

到目前为止,据我所知,您在注销时遇到了问题. 也许您将鼠标悬停在错误的元素上.或者尝试在Logout div出现之前单击它.

So far from what I understand you have problem with logging out. Maybe you hovered to the wrong element. Or tried to click Logout div before it appeared.

登录此代码足以使我注销(关闭所有弹出窗口之后)

Once I logged in this code was enough for me to log out (after closing all pop ups)

account_div = driver.find_element_by_xpath(
    '//li[contains(@class, "navbar__link--account")]//div[contains(@class, "navbar__link-text")]'
)
#
ActionChains(driver).move_to_element(account_div).perform()

logout_button = driver.find_element_by_xpath('//div[text()="Logout"]')
logout_button.click()

更多提示.最好使用一次driver.implicitly_wait来设置睡眠,而不是使用睡眠 因此,例如,如果您设置driver.implicitly_wait(5)并想通过xpath查找元素,它将等待5秒钟,然后再引发错误.由于无需显式等待n秒,因此可以节省时间.

Once more tip. Instead of using sleep it's better to set once driver.implicitly_wait So for example if you set driver.implicitly_wait(5) and you want to find element by xpath it will wait up till 5 seconds before raising an error. It helps to save time as you don't need to explicitly wait n seconds.

这篇关于创建登录和注销循环Python Selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆