簡體   English   中英

單擊按鈕會刷新頁面(意外),其中包含 selenium python

[英]Clicking a button refreshes a page (unintended) with 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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM