selenium定位无法定位网页元素

selenium打开网址之后。定位元素。

网页里这样的表格。我用了driver.find_elements_by_css_selector('[style="text-align: right;"]')[0].click()

去点击。

出错代码。

 Message: element click intercepted: Element <input type="text" class="unit sc-common-input" style="text-align: right;"> is not clickable at point (833, 869). Other element would receive the click: <div class="footer-buttons">...</div>

消息:元素单击被拦截

这个问题已经解决了。使用

div=driver.find_elements_by_css_selector('[style="text-align: right;"]')[1]
js1 ="arguments[0].scrollIntoView();" 
driver.execute_script(js1, div) 

可以定位输入。

但是出现了另外的问题,

 

.

这个页面可以定位到。

driver.find_elements_by_css_selector('[class="html-area-content"]')[0].click()

可以定位到。

但是无法传值进去

出现错误,元素不可交互。

是什么情况。

这个是html代码的输入框。

无法传值

您需要在该元素上使用焦点或滚动。您可能还必须使用显式等待。

 

WebElement firstbutton= driver.findElement(By.xpath("Your Element"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();

 

或者

该元素不可点击,因为其上方有一个微调器/叠加层:

 

By loadingImage = By.id("loading image ID");
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(ExpectedConditions.invisibilityOfElementLocated(loadingImage));

 

或者

 

Point p= element.getLocation();
Actions actions = new Actions(driver);
actions.moveToElement(element).movebyoffset(p.x,p.y).click().perform();

 

或者

如果仍然无法使用,请使用 JavascriptExecutor

 

JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", firstbutton);

根据报错的来看提示的是你定位的元素不能点击,其他元素<div class="footer-buttons">...</div>可以接收点击

说下解决思路吧,根据定义到的问题选择解决办法

1、先确保你要定位的元素是否正确的---通过chrome-f12下CTRL+F查询style="text-align: right;"]查看是否能定位正确,如下所示,可以看到能定位到几个,判断你取的find_elements返回值的第一个是否正确

2、如果定位的元素正确,并且已能定位到,需要进行以下几点排查

  •    元素是否接受click事件
  •   是否被其他元素遮挡导致不能点击---解决办法:可以判断遮挡的元素消失后再点击、或者使用js进行点击
  •   元素是否展示在当前屏幕还是需要滚动才能展示--解决办法:用js或者滚动到此元素显示在屏幕上

不过你是要输入内容,不用click,直接send_keys应该就可以了吧

   

哪个网站

你是怎么传值的呢

有4种可能的解决方案:
1.使用Actions()方法

 

WebElement element = driver.findElement(By("element_path"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();

 

2.在执行单击之前,使用“等待”使页面完全加载

 

driver.manage().timeouts().implicitlywait(15 TimeUnit.seconds)

 

3.由于元素上方有一个微调器/叠加层,因此该元素不可单击:

 

By loadingImage = By.id("loading image ID");
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(ExpectedConditions.invisibilityOfElementLocated(loadingImage));

4.宏执行

 

button = driver.find_element_by_xpath("xpath")
driver.execute_script("arguments[0].click();", button)