from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
options = webdriver.ChromeOptions()
# 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium
options.add_experimental_option('excludeSwitches', ['enable-automation'])
driver = webdriver.Chrome(options=options)
options = webdriver.ChromeOptions()
chains = ActionChains(driver)
login_url = "https://gsp.lazada-seller.cn/page/login"
username = "" # 需要正确的账号,才能登陆成功
password = "" # 需要正确的密码,才能登陆成功
driver.set_window_size(1500,1000)
delect = "1005003197347986"
def login(url):
driver.get(url=url)
# time.sleep属于强制等待,模拟人的等待行为
time.sleep(3)
# 找到用户名输入框,输入账号
login_name = driver.find_element_by_id("fm-login-id")
# 输入账号
login_name.send_keys(username)
time.sleep(4)
# 找到密码输入框,输入密码
login_password = driver.find_element_by_id("fm-login-password")
# 输入密码
login_password.send_keys(password)
# 找到登陆框
login_button = driver.find_element_by_id("fm-login-submit")
# 通过链的方式模拟登陆
chains.move_to_element(login_button)
time.sleep(2)
chains.click(login_button)
time.sleep(2)
chains.perform()
time.sleep(2)
# 刷新一下
driver.refresh()
time.sleep(2)
# ______________________________
# 找到用户名输入框,输入账号
login_name = driver.find_element_by_id("fm-login-id")
# 输入账号
login_name.send_keys(username)
time.sleep(4)
# 找到密码输入框,输入密码
login_password = driver.find_element_by_id("fm-login-password")
# 输入密码
login_password.send_keys(password)
# 找到登陆框
login_button = driver.find_element_by_id("fm-login-submit")
# 通过链的方式模拟登陆
chains.move_to_element(login_button)
time.sleep(2)
chains.click(login_button)
time.sleep(2)
chains.perform()
time.sleep(3)
# chains.move_to_element( driver.find_element_by_xpath("//li[@title='Promotions']/div/span/a/text()")).perform() # 主页右边活动入口
# chains.move_to_element(driver.find_element_by_xpath("//li[@title='Promotions']/div/span/a[normalize-space(.)='Promotions']")).perform()
# chains.move_to_element(driver.find_element_by_xpath("//li[@title='Promotions']/div/span/a[text()='Promotions']")).perform()
# chains.click(driver.find_element_by_xpath("//a[@data-aplus-ae='x15_580f813d']/text()")) #右侧view 入口
# WebElementsearchItemByText = driver.findElement(By.xpath("//*[@id='popover-search']/div/div/ul/li[1]/a/span[contains(text()='Some text')]"))
# chains.click(options.find_elemet_by_xpath("//*[data-spm-anchor-id="a1z10uk.home.navi_left_sidebar.droot_normal_promotions_campaignmanagement"]") )
chains.move_to_element( driver.find_element_by_xpath("//a[contains(text(),'Products')]")).perform()
# chains.move_to_element(driver.find_element_by_link_text("Products")).perform()
time.sleep(2)
driver.find_element_by_xpath("//a[@data-spm-anchor-id='a1z10uk.home.navi_left_sidebar.droot_normal_products_manageglobalproducts']").click()
# for handle in driver.window_handles:
# 先切换到该窗口
# driver.switch_to.window(handle)
# 得到该窗口的标题栏字符串,判断是不是我们要操作的那个窗口
# if 'Seller Center' in driver.title:
# 如果是,那么这时候WebDriver对象就是对应的该该窗口,正好,跳出循环,
# break
# time.sleep(2)
driver.find_element_by_xpath("//input[@id='sellerSku']").send_keys(delect) #输入产品id
time.sleep(2)
driver.find_element_by_xpath(
"//button[@class='next-btn next-btn-primary next-btn-medium search-button']/i").click()
time.sleep(2) # 搜索框
driver.find_element_by_xpath(
"//*[@id='pagination_id']//span[@class='next-select-inner']").click()
time.sleep(2) # 每页显示数量
driver.find_element_by_xpath(
"//ul[@class='next-menu-content']/li[3]").click()
time.sleep(8) # 50页
driver.find_element_by_xpath(
"//th[@rowspan=1]/div/label/input").click()
time.sleep(2) # select all
driver.find_element_by_xpath(
"//*[contains(text(),'Delete Product')]").click()
time.sleep(2) # delete product
driver.find_element_by_xpath(
"//*[@id='dialog-footer-2']/button[1]").click()
time.sleep(10) # remove
def main():
login(login_url)
if __name__ == '__main__':
main()