Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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
如何在Rselenium R中捕获动态xpath id_R_Xpath_Rselenium - Fatal编程技术网

如何在Rselenium R中捕获动态xpath id

如何在Rselenium R中捕获动态xpath id,r,xpath,rselenium,R,Xpath,Rselenium,我有一个关于动态xpath的问题 我试图用不同的方法来解决这个问题,但是,我还没有找到一个很好的解决办法 问题是,我必须使用一个xpath,不幸的是,它是动态的,并且长度不一样 这里是生成单击的html代码部分 <div class="field"> <a onclick="sendGaSearch();" class="ui button rounded" style="background- color: #3fa9f5;" id="id1a2"

我有一个关于动态xpath的问题 我试图用不同的方法来解决这个问题,但是,我还没有找到一个很好的解决办法

问题是,我必须使用一个xpath,不幸的是,它是动态的,并且长度不一样

这里是生成单击的html代码部分

   <div class="field">
        <a onclick="sendGaSearch();" class="ui button rounded" style="background- 
 color: #3fa9f5;" id="id1a2" href="javascript:;">
我从页面中提取的html代码部分如下:

 "\" style=\"background-color: #3fa9f5;\" id=\"id1a2\"
 href=\"javascript:;\">\n\t\t\t\t\t\t\tSearch\n\t\t\t\t\t\t</a>\n\t\t\t\t\t</div>\n\n\t\t\t\t</form>\n\t\t\t\t\n\t\t\t\t<script
“\”样式=\”背景色:\”3fa9f5;\“id=\”id1a2\”

href=\“javascript:;\”>\n\t\t\t\t\t\t\t搜索\n\t\t\t\t\t\t\t\t\t\t\t\t\n\n\t\t\t\t\t\t\n\t\t\t\t为什么要基于动态属性创建选择器

在这种情况下,您应该始终使用属性中的值,这些值不会发生变化,如果可能,还应使用有意义的文本,而不是过于笼统的内容:

 //a[@onclick='sendGaSearch()']

或者css:
a[href*=sendGaSearch]

我用这种方法解决了这个问题

  cod_html<-webElem$getPageSource()
  x<-str_extract (string = cod_html, pattern = ("(?<=;\" id=\").*(?=\" href=\"javascript)"))
  cod_c2<-paste0("#",x)
  webElem <- remote_driver$findElement(using = "css selector",value = cod_c2)
  webElem$clickElement()

谢谢你的回答。webElem在页面中测试xpath,然后确保元素可用或等待它;您会遇到什么错误?错误是:Selenium消息:无法找到元素://a[@onclick=“sendGaSearch()”]错误:摘要:NoTouchElement详细信息:无法使用给定的搜索参数在页面上找到元素。class:org.openqa.selenium.NoSuchElementException进一步详细信息:运行errorDetails方法请检查并测试选择器,然后查看它是否是一个选择器问题(因为某些内容无效),或者它是否是一个与页面加载相关且未等待元素的问题。还要查看它是否找到多个元素,如果是,则您的元素应该是第一个找到的元素。还要检查iframe,如果元素位于其内部,则iframe您需要首先切换到iframe
  cod_html<-webElem$getPageSource()
  x<-str_extract (string = cod_html, pattern = ("(?<=;\" id=\").*(?=\" href=\"javascript)"))
  cod_c2<-paste0("#",x)
  webElem <- remote_driver$findElement(using = "css selector",value = cod_c2)
  webElem$clickElement()