在 python 自动化中完成的任务
Task done in python automation
正如标题所说,我正在做一个我需要做的小项目
“打开 link,单击一个按钮,替换 link 的一部分并再次执行任务”
我的代码有点乱,我还是个新手,但到此为止,我 运行 遇到的问题是添加 [= 时页面打不开16=] 函数。页面打不开,我可以不用 if else,但由于按钮的 xpath 在随机编号的页面中发生变化,这是我能想到的唯一解决方案
PATH = Service("C:\Program Files (x86)\chromedriver.exe")
driver = webdriver.Chrome(service=PATH)
driver.maximize_window()
button = driver.find_element_by_xpath("/html/body/div[1]/div[1]/main/div/div/div/div[1]/div/div[1]/div[2]/div[1]/div/section/div[2]/div[3]/div/div[2]/button")
button1 = driver.find_element_by_xpath("/html/body/div[1]/div[1]/main/div/div/div/div[1]/div/div[1]/div[2]/div[1]/div/section/div/div[3]/div/button")
for i in range(1,5):
print(f"https://test.com/page/{i}")
driver.get(f"https://test.com/page/{i}")
if button is not None:
button.click()
else:
button1.click()
在获取按钮之前加载页面。另外,确保按钮加载了这个脚本:
PATH = Service("C:\Program Files (x86)\chromedriver.exe")
driver = webdriver.Chrome(service=PATH)
driver.maximize_window()
button_selector = "/html/body/div[1]/div[1]/main/div/div/div/div[1]/div/div[1]/div[2]/div[1]/div/section/div[2]/div[3]/div/div[2]/button"
button1_selector = "/html/body/div[1]/div[1]/main/div/div/div/div[1]/div/div[1]/div[2]/div[1]/div/section/div/div[3]/div/button"
for i in range(1, 5):
print(f"https://test.com/page/{i}")
# Load the page
driver.get(f"https://test.com/page/{i}")
# Get the buttons
button = driver.find_element_by_xpath(button_selector) or None
button1 = driver.find_element_by_xpath(button1_selector) or None
time.sleep(1) # Make sure the buttons are loaded
if button is not None:
button.click()
elif button1 is not None:
button1.click()
driver.quit() # Be sure to close the driver
正如标题所说,我正在做一个我需要做的小项目 “打开 link,单击一个按钮,替换 link 的一部分并再次执行任务”
我的代码有点乱,我还是个新手,但到此为止,我 运行 遇到的问题是添加 [= 时页面打不开16=] 函数。页面打不开,我可以不用 if else,但由于按钮的 xpath 在随机编号的页面中发生变化,这是我能想到的唯一解决方案
PATH = Service("C:\Program Files (x86)\chromedriver.exe")
driver = webdriver.Chrome(service=PATH)
driver.maximize_window()
button = driver.find_element_by_xpath("/html/body/div[1]/div[1]/main/div/div/div/div[1]/div/div[1]/div[2]/div[1]/div/section/div[2]/div[3]/div/div[2]/button")
button1 = driver.find_element_by_xpath("/html/body/div[1]/div[1]/main/div/div/div/div[1]/div/div[1]/div[2]/div[1]/div/section/div/div[3]/div/button")
for i in range(1,5):
print(f"https://test.com/page/{i}")
driver.get(f"https://test.com/page/{i}")
if button is not None:
button.click()
else:
button1.click()
在获取按钮之前加载页面。另外,确保按钮加载了这个脚本:
PATH = Service("C:\Program Files (x86)\chromedriver.exe")
driver = webdriver.Chrome(service=PATH)
driver.maximize_window()
button_selector = "/html/body/div[1]/div[1]/main/div/div/div/div[1]/div/div[1]/div[2]/div[1]/div/section/div[2]/div[3]/div/div[2]/button"
button1_selector = "/html/body/div[1]/div[1]/main/div/div/div/div[1]/div/div[1]/div[2]/div[1]/div/section/div/div[3]/div/button"
for i in range(1, 5):
print(f"https://test.com/page/{i}")
# Load the page
driver.get(f"https://test.com/page/{i}")
# Get the buttons
button = driver.find_element_by_xpath(button_selector) or None
button1 = driver.find_element_by_xpath(button1_selector) or None
time.sleep(1) # Make sure the buttons are loaded
if button is not None:
button.click()
elif button1 is not None:
button1.click()
driver.quit() # Be sure to close the driver