python实现循环注册登录_创建登录和注销循环Python Selenium

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

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:

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?

解决方案

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()

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值