奇怪的 Python Selenium 按钮点击行为 [英] Weird Python Selenium Button Click Behaviour
问题描述
我要点击的部分:
<ul class="btns right">
<li><a href="javascript:void(0)" onclick="hr_expand_event_tab_all("")" class="expand-all" id="btn_expand_all_10580503">View All Cards</a></li>
</ul>
我想的很简单.但我好像遗漏了什么.
Pretty straightforward I thought. But I seem to be missing something.
问题现已更新到页面下方.xpath 不是我用更正的 xpath 尝试过的问题,它与使用类名相同.CSS 隐藏了多个版本的按钮,但在它确实使用 xpath 或类名找到的按钮上抛出了一个 common.exception.
我已经检查过页面是否正确加载并且元素在那里.我有一个检查要等到整个页面加载完毕,然后才能确保截图.
I've checked the page is loaded properly and the element is there. I have a check to wait until the full page is loaded and it screenshots to be sure.
loadbutton = Driver.find_element_by_xpath("//a[@class='expand-all']")
给出:
<class 'selenium.common.exceptions.ElementNotVisibleException'>
所以我试图找到一个带有锚点的点击:
So I tried to find an onclick with the anchor:
loadbutton = Driver.find_element_by_xpath("//li[contains(@onclick, 'View All Cards')]")
同样的结果.我也尝试了一些正则表达式来捕捉 id 变化,但我不确定我哪里出错了.有一个 onlick,它已加载,但我找不到它.
With the same outcome. I've tried a bit of regex to catch the id variations as well but I'm not sure where I'm going wrong here. There's an onlick and it is loaded but I can't see to find it.
如果有人能告诉我我在这个问题上做错了什么,我将不胜感激.
I'd appreciate anyone who can show me what I'm doing wrong on this one.
/更新:
原来有多个版本的按钮,有些是可见的,有些是不可见的.
Turns out there's multiple versions of the button some are visible and others are not.
我循环了:
loadbutton = Driver.find_elements_by_xpath("//a[@class='expand-all']")
for button in loadbutton:
print "button found"
它出现了多个结果.较早的那些被隐藏了,但最后的那些肯定会显示在我的浏览器和屏幕截图上.所以我预计早期的会失败,并添加了一个 .click() 和 try: except: ,但它们仍然失败了.没想到.
It turned up multiple results. The earlier ones are hidden but the ones at the end are certainly showing on my browser and the screenshot. So I expected the early ones to fail and added a .click() with a try: except: and they all failed still. Didn't expect that.
进一步更新:
所以我运行了这个:
loadbutton = Driver.find_elements_by_xpath("//a[@class='expand-all']")
for button in loadbutton:
print "button found"
try:
button.click()
except:
e = sys.exc_info()[0]
print e
第一对夫妇给了我这个:
The first couple gave me this:
<class 'selenium.common.exceptions.ElementNotVisibleException'>
好的,预计 CSS 会隐藏它.显示的最后两个给出了这个:
OK expected the CSS is hiding it. The last two which are displaying gave this:
<class 'selenium.common.exceptions.WebDriverException'>
所以它可以看到它们.它不会点击它们.常见例外"似乎并没有太大帮助.
So it can see them. It won't click them. "Common exception" doesn't seem overly helpful.
推荐答案
试试这个 xpath(更新代码块 SO 站点删除了我的 *)
Try this xpath (updated with code block SO site removed my *)
//*[contains(concat(' ', @class, ' '), ' btns right ')]//*[contains(concat(' ', @class, ' '), ' expand-all ') 和 contains(text(), '查看所有卡片')]
提供一些等待元素可点击(推荐隐式).
Provide some wait for the element to be clickable(implicit is recommended).
我只用于 java ,但我在这里指的是 python 这里可能有帮助!!
I have Used Only for java , But I refered here for python here it may help!!
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
button = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[contains(concat(' ', @class, ' '), ' btns right ')]//*[contains(concat(' ', @class, ' '), ' expand-all ') and contains(text(), 'View All Cards')]')))
button.click()
Even if the above thing fails, try this
driver.execute_script("document.getElementsByClassName('expand-all')[0].click();")
在所需元素上注入一个人工点击,删除(注释)所有其他代码
inject an artificial CLICK on the desired element, remove(comment) all other codes
可能是你的应用属于link2 OP :)
May be ur app falls under link2 OP :)
这篇关于奇怪的 Python Selenium 按钮点击行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!