简体   繁体   English

单击方法不起作用 - 使用 selenium webdriver 和 java

[英]Click method not working - using selenium webdriver with java

I'm trying to scrape data from the public site.我正在尝试从公共网站上抓取数据。 On the search page enter the search value and press the view button (normal submit)its shows the results.在搜索页面输入搜索值并按下查看按钮(正常提交)它会显示结果。

normal browser view button clicks and shows the report as expected.正常浏览器视图按钮单击并按预期显示报告。 but when I try to click the same button via the Webdriver code, the button clicks but the result was not shown.但是当我尝试通过 Webdriver 代码单击相同的按钮时,该按钮会单击但未显示结果。

Can anyone face this issue?任何人都可以面对这个问题吗?

I tried the button click using XPath( //*[@id="b-form-submit"]/div/button ) with the below method我尝试使用 XPath( //*[@id="b-form-submit"]/div/button ) 使用以下方法单击按钮

Click()
Submit()
Click using Javascript
Click using action class (keyboard event)

Button HTML code按钮 HTML 代码

<div class="b-button" id="b-form-submit">
   <div class="b-button-container">
      <span class="left">
      <i></i>
      </span>
      <button alt="View" type="submit">View</button>
      <span class="right">
      <i></i>
      </span>
   </div>
</div>

When i try to click the view button manually from the webdriver (debug mode) its not wokring, but same its working normal web browser (all the browsers)当我尝试从网络驱动程序(调试模式)手动单击查看按钮时,它不会工作,但它的工作正常 web 浏览器(所有浏览器)

The <button> isn't an immediate descendant of the <div> you can use either of the following Locator Strategies :: <button>不是<div>的直接后代,您可以使用以下任一定位器策略::

  • css_selectors : css_selectors

     div#b-form-submit>div button[alt='View']
  • xpath : xpath

     //div[@id="b-form-submit"]/div//button[@alt='View' and text()='View']

So to click() on the element with text as View you can use either of the following Locator Strategies :因此,要在元素上以文本作为Viewclick() ,您可以使用以下任一Locator Strategies

  • cssSelector : cssSelector

     driver.findElement(By.cssSelector("div#b-form-submit>div button[alt='View']")).click();
  • xpath : xpath

     driver.findElement(By.xpath("//div[@id="b-form-submit"]/div//button[@alt='View' and text()='View']")).click();

Ideally to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies :理想情况下,要在需要为elementToBeClickable()诱导WebDriverWait的元素上click() ,您可以使用以下任一定位器策略

  • cssSelector : cssSelector

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#b-form-submit>div button[alt='View']"))).click();
  • xpath : xpath

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id="b-form-submit"]/div//button[@alt='View' and text()='View']"))).click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM