曾经试图让它工作几个小时。 这是我的第一个真正的python项目,是的,很乐意提供帮助。
HTML
<input type="button" id="lyca_cart_newsim_button1" value="FORTSÆT" class="et_pb_more_button et_pb_button lyca_cart_topup_summary" onclick="nc_newsim_open_tab2('payment','sid','tid')">
xpath
//*[@id='lyca_cart_newsim_button1']
这会产生错误元素“不可交互”
driver.find_element_by_xpath("//*[@id='lyca_cart_newsim_button1']").click()
这不会产生任何错误,但不会单击按钮
element = driver.find_element_by_xpath("//*[@id='lyca_cart_newsim_button1']")
driver.execute_script("arguments[0].click();", element)
这次超时
WebDriverWait(driver, 10).until(EC.element_to_be_clickable
第一个给element not interactable
第二个没有错误。
我在代码的早期使用了它,并且在这里可以正常工作,
element = driver.find_element_by_xpath("//*[@id='lyca_cart_newsim_button1']")
driver.execute_script("arguments[0].click();", element)
答案 0 :(得分:0)
我从未使用硒的预期条件来获得好运,尤其是等待元素可单击。无论是否最佳,我所做的都是设法让循环单击并在一定时间内继续尝试。这是我在C#中使用的:
int timeToTryMilliseconds = 5000;
bool timeNotExpired = true;
Stopwatch sw = new Stopwatch();
sw.Start();
while (timeNotExpired)
{
try
{
driver.FindElement(By.XPath("//*[@id='lyca_cart_newsim_button1']").click()
break;
}
catch
{
// Half second wait, so it's not polling constantly
System.Threading.Thread.Sleep(500);
timeNotExpired = timeToTryMilliseconds > sw.ElapsedMilliseconds;
}
}
如果有更好的方法,我很乐意使用它。
答案 1 :(得分:0)
请确认该按钮位于iframe代码中。如果在iframe中,则必须切换到iframe
如果它不在iframe中,请尝试使用以下代码,它可能会起作用
driver.execute_script("$('#lyca_cart_newsim_button1').click()");