python - 单击按钮会使用 selenium python 刷新页面(意外)
问题描述
我正在尝试自动将文件上传到我的 github 存储库。
代码:
from selenium import webdriver
browser=webdriver.Chrome(r'C:\Users\Eliran\Desktop\chromedriver')
browser.get('https://github.com/login')
username=browser.find_element_by_id('login_field')
username.send_keys(myemail)
password=browser.find_element_by_id('password')
password.send_keys(mypassword)
sign_in_btn=browser.find_element_by_name('commit')
sign_in_btn.click()
browser.get('https://github.com/DevEliran/DevEliran.github.io/upload/master') #pathing immidiately to "upload files" in my repo
commit=browser.find_elements_by_xpath("/html/body/div[4]/div/main/div[2]/div/form/button")[0] # commit button
upload=browser.find_element_by_class_name('manual-file-chooser')#manually uploading my file
upload.send_keys(myfilepath)
commit.click()
commit.click()
页面刷新并显示错误后:“添加一些文件以包含在此提交中。”
我已经仔细检查了每个元素。有没有人遇到过同样的问题?
解决方案
你能检查文件路径是否真的存在吗?
if (!os.path.exists(myfilepath)):
print(myfilepath + ' doesnt exist')
else:
print(myfilepath + ' exists')
upload.send_keys(myfilepath)
commit.click()
如果文件存在,则有可能将其发送到不正确的元素。尝试先将路径发送到此 xpath,然后等待它上传,然后找到并单击提交元素。
browser.find_elements_by_xpath("//input[@type='file']").send_keys(myfilepath)
wait = WebDriverWait(browser, 90)
men_menu = wait.until(ec.visibility_of_element_located((By.XPATH, "//table[@class='files']//td[@class='name']")))
browser.find_elements_by_xpath("//button[contains(text(), 'Commit')]").click()