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.