Python的硒不能在点击链接(不里面的iframe)

Python的硒不能在点击链接(不里面的iframe)

问题描述:

有网页上的按钮,看起来像这样:Python的硒不能在点击链接(不里面的iframe)

<button class="WAXG WEXG WKKH WOWG WPO" tabindex="0" data-automation-activebutton="true" aria-hidden="false" aria-disabled="false" data-automation-id="wd-ActiveList-addButton" role="button" data-automation-button-type="AUXILIARY" title="Add" type="button"><span class="WFXG WBXG"></span><span class="WCXG" title="Add">Add</span></button> 

我用下面的代码按一下按钮:

xpath = "//button[@data-automation-id='wd-ActiveList-addButton']" 
add = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, xpath))) 
add.click() 

它总是会导致以下错误:

selenium.common.exceptions.TimeoutException: Message: 

我已经用不同的方式来寻找元素,然后单击尝试,但总是得到相同的错误。该按钮不在iframe内。此外,我可以访问/点击按钮周围的所有元素。由于错误信息是空的,我无法确定为什么发生这种情况。

编辑

下面是来自检查者周围的一些代码:

<div class="WF-M WFN WOYM WEYM" id="wd-SectionView-NO_METADATA_ID"> 
    <div class="WH-M"> 
     <div class="WOO WFN" data-automation-id="activeList" id="wd-ActiveList- 6$87772"> 
      <div class="WHP"> 
      </div> 
      <button class="WAXG WEXG WKKH WOWG WPO" tabindex="0" data-automation-activebutton="true" aria-hidden="false" aria-disabled="false" data-automation-id="wd-ActiveList-addButton" role="button" data-automation-button-type="AUXILIARY" title="Add" type="button"> 
       <span class="WFXG WBXG"></span> 
       <span class="WCXG" title="Add">Add</span> 
      </button> 
     </div> 
    </div> 
</div> 
+1

如果你尝试一下跨度而不是按钮? 'xpath =“// span [@ title ='Add']”'?有可能这个按钮实际上不可见或者大小为0。这里不太可能给出'html',但是可能的。您也可以尝试使用devtools或firepath来查看将鼠标放在按钮上时突出显示的元素,因为它实际上可能是包含该按钮的元素。 – mrfreester

+2

检查这是否是唯一具有指定选择器的元素print(len(driver.find_elements_by_xpath(“// button [@ data-automation-id ='wd-ActiveList-addButton']”)))'。可能有同样的隐藏按钮 – Andersson

+0

@mrfreester我仍然使用'xpath =“// span [@ title ='Add']”'得到相同的错误。当我点击按钮时,Firepath会为按钮生成以下xpath:'.//*[@ id ='wd-ActiveList-6 $ 87772']/button'。 – raul

正如我已经承担了意见有两个页面上的按钮,可以通过属性data-auto‌​mation-id='wd-Active‌​List-addButton'发现:第一是隐藏。这就是为什么你的期望等待,直到它变得可见总是返回False

您可能需要使用下面的代码:

xpath = "(//button[@data-automation-id='wd-ActiveList-addButton'])[2]" 
add = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, xpath))) 
add.click() 

它应该让你点击可见"Add"按钮