Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从所有类都相同的指定类获取文本-Selenium Javascript_Javascript_Node.js_Selenium Chromedriver - Fatal编程技术网

从所有类都相同的指定类获取文本-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框架。