为什么我不能在selenium webdriver python上单击此按钮?

2024-10-03 04:36:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法单击路由器界面中的某些按钮。我只能使用pyautogui点击浏览。但这种方法不起作用。如何在Selenium上单击此按钮?我将使用此代码重置我的ip地址

这是我要单击的位置的css代码:

<a href="#" class="edit" id="editBtn" title="Düzenle" onclick="editClick('ppp1.1', 'MyISP_PTM_35')"></a>

Html数据: https://mega.nz/file/2XJyEbCR#xBcEtzYh8QFLWTmSfAqll2V-p-SHiaw4wEz1RAWtso0

我尝试了所有的方法,但没有奏效

try:
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#editBtn'))).send_keys("\n")
except:
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#editBtn'))).send_keys(Keys.ENTER)
try:
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#editBtn')))[0].send_keys("\n")
except:
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#editBtn')))[0].send_keys(Keys.ENTER)
try:
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#editBtn')))[0].click()
except:
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#editBtn'))).click()

enter image description here


Tags: tosendbydriverelementbekeysselector
2条回答

我解决了。我应该用相框的

#Selenium
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver import Firefox
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions.interaction import KEY
#Beautiful Soup
from bs4 import BeautifulSoup
import lxml.html

import time

import pyautogui


def router_reset():
    
    print ("Modem resetleniyor")
    
    driver = webdriver.Chrome('C:/Anaconda3/chromedriver.exe')
    driver.get('http://192.168.1.1/login.cgi')
    
    username = driver.find_element_by_id('AuthName')
    password = driver.find_element_by_id('AuthPassword')
    login = driver.find_element_by_xpath("//*[@id='login']/fieldset/ul/li[6]/input")
    
    username.send_keys("admin")
    password.send_keys("turktelekom")
    login.click()
    
    time.sleep(1)
    #Açılan Ekranı Atla Tuşu
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="login"]/fieldset/ul/li[3]/input[2]'))).click()
    

    source = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="network"]')))
    #target = driver.find_element_by_id("div2")
    target = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="network"]')))
    # Create the object for Action Chains
    actions = ActionChains(driver)
    actions.drag_and_drop(source, target)
    # perform the operation on the element
    actions.click(target)
    actions.perform()

    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#network-broadband > a'))).click()

    time.sleep(2)
    WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'//*[@id="mainFrame"]')))
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#editBtn'))).click()

    driver.switch_to.default_content()
    time.sleep(5)
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'body > div.ui-dialog.ui-widget.ui-widget-content.ui-corner-all > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > button:nth-child(2)'))).click()


    time.sleep(60)
    print("Modeme Reset Atıldı, 60sn Bekleme Süresi Başladı.")
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="logoutName"]'))).click()
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'body > div.ui-dialog.ui-widget.ui-widget-content.ui-corner-all > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > button:nth-child(2)'))).click()

router_reset() 

试试这个

link = driver.find_element_by_link_text('')
link.click()

如果要单击链接, 也许这个例子对你有帮助

相关问题 更多 >