页面上有2个按钮,这两个按钮之间的区别是"onclick"
。
<button class="btn btn-primary" style="width: 96px;" type="button" id="YesBtn" onclick="check_security('security_div0')">OK</button>
<button class="btn btn-primary" style="width: 96px;" type="button" id="YesBtn" onclick="check_security('wlan1security_div0')">OK</button>
我当时正在考虑使用xpath
:
driver.find_element_by_xpath("//form[@id='update-container-id']/div/div/div/div[2]/div/div[2]/table[1]/tbody/tr[1]/td[8]/div[3]/div/div/div/div[3]/button").click()
但是它响应以下错误:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <button id="YesBtn" class="btn btn-primary" type="button"> could not be scrolled into view
有人可以帮助我正确单击第二个按钮吗?非常感谢。
答案 0 :(得分:0)
尝试使用x路径import random
import numpy as np
import math
def sampling(a, b, Ip, In, alpha, elements):
#print("sampling")
result = []
print("a:", a)
print("b:", b)
print("alpha:", alpha)
print("elements:", elements)
print("Ip:", Ip)
print("In:", In)
summary = 0
N = len(alpha)
for i in range(N):
A = 0
B = 0
value = 0
'''
if i == elements:
continue
'''
for j in range(elements):
print("A:")
print("1:", a[i][j] + Ip[j], ",2:", b[i][j] + In[j], ",3:", a[i][j] + b[i][j] + Ip[j] + In[j])
A += math.lgamma(a[i][j] + Ip[j]) + math.lgamma(b[i][j] + In[j]) - math.lgamma(a[i][j] + b[i][j] + Ip[j] + In[j])
print("B:")
print("1:", a[i][j] + b[i][j], ",2:", a[i][j], ",3:", b[i][j])
B += math.lgamma(a[i][j] + b[i][j]) - math.lgamma(a[i][j]) - math.lgamma(b[i][j])
print("A:", A)
print("B:", B)
print("value:", value)
value += math.log(alpha[i]) + A + B
print("i:", i, "value:", value)
summary += math.exp(value)
result.append(math.exp(value))
print("percentage:", np.array(result) / summary)
#print("summary:", summary)
choose = random.uniform(0.0, summary)
#print("choose:", choose)
num = N - 1
for i in range(N):
choose -= result[i]
if choose <= 0:
num = i
break
'''
if num >= elements:
num += 1
'''
return num
def delta(x, y):
if x == y:
return 1
return 0
data = []
print("please input (k, l):")
num = map(int, raw_input().split())
k_l = k = num[0]
l_l = l = num[1]
param_p = np.array([[0 for i in range(k)] for j in range(l)])
param_n = np.array([[0 for i in range(k)] for j in range(l)])
param_k = np.array([0] * k)
param_l = np.array([0] * l)
count = 1000
print("please input (N1, N2):")
number = map(int, raw_input().split())
N1 = number[0]
N2 = number[1]
random.randint(50, 100)
z1 = []
z2 = []
z1_sum = np.array([[[0 for i in range(k)] for j in range(l)] for n in range(N1)])
z2_sum = np.array([[[0 for i in range(k)] for j in range(l)] for n in range(N2)])
z1_zero = np.array([[[0 for i in range(k)] for j in range(l)] for n in range(N1)])
z2_zero = np.array([[[0 for i in range(k)] for j in range(l)] for n in range(N2)])
value = 0
valuesL = []
for i in range(N2):
valuesL.append(value)
if i % l == l - 1:
value = 0
else:
value += 1
valuesL = np.sort(np.array(valuesL))
for i in range(N2):
z2.append([valuesL[i], i],)
tempAlpha = 0.5
tempA = 0.5
tempB = 0.5
alpha1 = np.array([tempAlpha for i in range(k)])
alpha2 = np.array([tempAlpha for i in range(l)])
a = np.array([[tempA for i in range(k)] for j in range(l)])
b = np.array([[tempB for i in range(k)] for j in range(l)])
value = 0
valuesK = []
for i in range(N1):
valuesK.append(value)
if i % k == k - 1:
value = 0
else:
value += 1
valuesK = np.sort(np.array(valuesK))
for i in range(N1):
z1.append([valuesK[i], i],)
z1 = np.array(z1)
z2 = np.array(z2)
print(z1)
print(z2)
print("please input relation-data:")
for i in range(N2):
line = map(int, raw_input().split())
data.append(line)
#print(data)
for i in range(N2):
l = z2[i][0]
for j in range(N1):
k = z1[j][0]
z1_sum[j][l][k] += data[i][j]
z2_sum[i][l][k] += data[i][j]
z1_zero[j][l][k] += 1 - data[i][j]
z2_zero[i][l][k] += 1 - data[i][j]
param_p[l][k] += data[i][j]
param_n[l][k] += 1 - data[i][j]
param_k[k] += data[i][j]
param_l[l] += data[i][j]
#print("param_p:", param_p)
#print("param_n:", param_n)
#print("param_k:", param_k)
#print("param_l:", param_l)
#print("z1-sum:")
#print(z1_sum)
#print("z2-sum:")
#print(z2_sum)
#sampling(a, b, I, alpha, length, elements)
k = 0
l = 0
pastK = np.array([0 for i in range(N1)])
pastL = np.array([0 for i in range(N2)])
for n in range(count):
pastK[:] = z1[:, 0]
for i in range(N1): #sampling z1 objects
t_k = z1[i][0]
k = z1[i][0] = sampling((a[:, :] + param_p[:, :] - z1_sum[i, :, :]).T, (b[:, :] + param_n[:, :] - z1_zero[i, :, :]).T, np.sum(z1_sum[i, :, :], axis=1), np.sum(z1_zero[i, :, :], axis=1), alpha1[:] + param_k[:] - np.sum(z1_sum[i, :, :], axis=0), l_l)
#print("t_k:", t_k, "k:", k)
for i in range(N1):
t_k = pastK[i]
k = z1[i][0]
print("t_k:", t_k, "k:", k)
if t_k != k:
#change param_p param_n param_k
#print("old-z1:", z1)
print("param_p(past):", param_p)
print("param_n(past):", param_n)
print("param_k(past):", param_k)
z1_sum_temp = z1_sum[i, :, t_k] #reduce order
z1_zero_temp = z1_zero[i, :, t_k]
summary = np.sum(z1_sum_temp)
param_p[:, t_k] -= z1_sum_temp #minus old value
param_n[:, t_k] -= z1_zero_temp
param_k[t_k] -= summary
param_p[:, k] += z1_sum_temp #plus new value
param_n[:, k] += z1_zero_temp
param_k[k] += summary
print("param_p(now):", param_p)
print("param_n(now):", param_n)
print("param_k(now):", param_k)
#print("past-z2-sum:", z2_sum)
for j in range(N2):
l_j = z2[j][0]
z2_sum[j][l_j][t_k] -= data[j][i]
z2_sum[j][l_j][k] += data[j][i]
z2_zero[j][l_j][t_k] -= 1 - data[j][i]
z2_zero[j][l_j][k] += 1 - data[j][i]
#print("now-z2-sum:", z2_sum)
#print("past-z1-sum:", z1_sum)
#change z1_sum
z1_sum[i, :, k] = z1_sum[i, :, t_k]
z1_sum[i, :, t_k] = 0
z1_zero[i, :, k] = z1_zero[i, :, t_k]
z1_zero[i, :, t_k] = 0
#print("now-z1-sum:", z1_sum)
#print("new-z1:", z1)
pastL[:] = z2[:, 0]
for i in range(N2): #sampling z2 objects
t_l = z2[i][0]
l = z2[i][0] = sampling(a[:, :] + param_p[:, :] - z2_sum[i, :, :], b[:, :] + param_n[:, :] - z2_zero[i, :, :], np.sum(z2_sum[i, :, :], axis=0), np.sum(z2_zero[i, :, :], axis=0), alpha2[:] + param_l[:] - np.sum(z2_sum[i, :, :], axis=1), k_l)
print("t_l:", t_l, "l:", l)
for i in range(N2):
t_l = pastL[i]
l = z2[i][0]
print("t_l:", t_l, "l:", l)
if t_l != l:
#change param_p param_n param_l
#print("old-z2:", z2)
z2_sum_temp = z2_sum[i, t_l, :] #reduce order
z2_zero_temp = z2_zero[i, t_l, :]
summary = np.sum(z2_sum_temp)
param_p[t_l, :] -= z2_sum_temp #minus old value
param_n[t_l, :] -= z2_zero_temp
param_l[t_l] -= summary
param_p[l, :] += z2_sum_temp #plus new value
param_n[l, :] += z2_zero_temp
param_l[l] += summary
#print("param_p(past):", param_p)
#print("param_n(past):", param_n)
#print("param_l(past):", param_l)
print("param_p(now):", param_p)
print("param_n(now):", param_n)
print("param_l(now):", param_l)
#print("past-z1-sum(past):", z1_sum)
#print("past-z1-zero(past):", z1_zero)
#change z1_sum
for j in range(N1):
k_j = z1[j][0]
z1_sum[j][t_l][k_j] -= data[i][j]
z1_sum[j][l][k_j] += data[i][j]
z1_zero[j][t_l][k_j] -= 1 - data[i][j]
z1_zero[j][l][k_j] += 1 - data[i][j]
#print("now-z1-sum(now):", z1_sum)
#print("past-z1-zero(now):", z1_zero)
#print("past-z2-sum(past):", z2_sum)
#print("past-z2-zero(past):", z2_zero)
#change z2_sum
z2_sum[i, l, :] = z2_sum[i, t_l, :]
z2_sum[i, t_l, :] = 0
z2_zero[i, l, :] = z2_zero[i, t_l, :]
z2_zero[i, t_l, :] = 0
#print("now-z2-sum(now):", z2_sum)
#print("now-z2-zero(now):", z2_sum)
#print("new-z2:", z2)
#z = input()
print("Hello")
z1 = z1[z1[:, 0].argsort(), :]
z2 = z2[z2[:, 0].argsort(), :]
lists = np.array(data)
lists = lists[:, z1[:, 1]]
lists = lists[z2[:, 1], :]
print(z1)
print(z2)
print(lists)
//button[@onclick="check_security('wlan1security_div0')"]
使用Action类,
driver.find_element_by_xpath("//button[@onclick=\"check_security('wlan1security_div0')\"]").click()
使用Java脚本执行程序,
button = driver.find_element_by_xpath("//button[@onclick=\"check_security('wlan1security_div0')\"]")
ActionChains(driver).move_to_element(button).click(button).perform()
答案 1 :(得分:0)
根据您提供的 HTML ,要使用onclick()
事件单击按钮,可以使用以下解决方案:
第一个元素(css_selector):
driver.find_element_by_css_selector("button.btn.btn-primary#YesBtn[onclick*='security_div0']").click()
第一个元素(xpath):
driver.find_element_by_xpath("//button[@class='btn btn-primary' and @id='YesBtn'][@onclick=\"check_security('security_div0')\"]").click()
第二个元素(css_selector):
driver.find_element_by_css_selector("button.btn.btn-primary#YesBtn[onclick*='wlan1security_div0']").click()
第二个元素(xpath):
driver.find_element_by_xpath("//button[@class='btn btn-primary' and @id='YesBtn'][@onclick=\"check_security('wlan1security_div0')\"]").click()
答案 2 :(得分:0)
首先,您要使用非常长的xpath,这将很难维护。您可以进一步缩小范围。
现在,您可以尝试一些xpath:
1)获取ID为YesBtn
的 second 按钮(假设只有两个具有该属性的按钮):
driver.find_element_by_xpath("(//button[@id= 'YesBtn'])[2]");
2)通过onclick
属性查找:
driver.find_element_by_xpath("//button[@onclick= \'check_security(\'wlan1security_div0\')\']");
答案 3 :(得分:0)
button1 和 HTML :
<button class="btn btn-primary" style="width: 96px;" type="button" id="YesBtn" onclick="check_security('security_div0')">OK</button>
XPATH:
//button[text()='OK' and @onclick="check_security('security_div0')"]
按钮2 和 HTML :
<button class="btn btn-primary" style="width: 96px;" type="button" id="YesBtn" onclick="check_security('wlan1security_div0')">OK</button>
HTML:
//button[text()='OK' and @onclick="check_security('wlan1security_div0')"]
答案 4 :(得分:0)
try:
buttonVar = browser.find_element_by_css_selector(cssSel)
ActionChains(browser).move_to_element(buttonVar).click(buttonVar ).perform()
except Exception as ex:
print("button not found)
Firefox为您提供了几个可以与硒find_element或元素一起使用的选项;
我个人不喜欢xpath。 它们有时工作时间长且不规则,但任何适合您的风格。就我而言,css_selector可以正常工作。