从所有类都相同的指定类获取文本-Selenium Javascript
这是一个网页的照片,我想访问它的文本,但它们都有相同的类 在整个代码中有相同的名字和相同的东西 我尝试了从所有类都相同的指定类获取文本-Selenium Javascript,javascript,node.js,selenium-chromedriver,Javascript,Node.js,Selenium Chromedriver,这是一个网页的照片,我想访问它的文本,但它们都有相同的类 在整个代码中有相同的名字和相同的东西 我尝试了elem=driver.findElement(webdriver.By.className('col-sm-7')).text 但它不起作用 有人能帮我吗?如果我理解正确,您想获取一个特定的div的文本。在这种情况下,考虑这样的事情: //A function to be able to do this multiple times function getDivText(position
elem=driver.findElement(webdriver.By.className('col-sm-7')).text代码>
但它不起作用
有人能帮我吗?如果我理解正确,您想获取一个特定的div
的文本。在这种情况下,考虑这样的事情:
//A function to be able to do this multiple times
function getDivText(position, className) {
//getting all divs with the specified class...
var divs = document.getElementsByClassName(className);
//and picking the one with the specified position
var target = divs[position];
//If the div could be found, return its text
if(target) {
return target.innerText;
} else {
return ""; //For demonstration purposes, we just return an empty string if the wanted div wasn't found
}
}
然后在需要时调用该函数。例如:
getDivText(“我的div类”,2)代码>您编写的代码在哪里?我尝试了elem=driver.findElement(webdriver.By.className('col-sm-7')).text;哪一个“col-sm-7”是另一个div。但是它不工作,所以你从哪里得到.col-sm-7
?请发布一个完整的可复制的示例这是selenium的问题,不是普通的浏览器DOMIt不工作,即使我对它进行了一些编辑,以使用selenium框架。