0

Im trying to scrape some websites, but sometimes theres some that do not contain a button that the Code look for with findElement. So when the Code doesn't find what im looking for, it gives me the following error:

Selenium message:no such element: Unable to locate element: {"method":"link text","selector":"see more"}
  (Session info: chrome=104.0.5112.79)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '4.0.0-alpha-2', revision: 'f148142cf8', time: '2019-07-01T21:30:10'
System info: host: '192.168.1.12', ip: 'fe80:0:0:0:4c4:faba:fe32:b5f4%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '12.5', java.version: '1.8.0_341'
Driver info: driver.version: unknown

I tried using findElements but after that its supposed to click the Element, so not possible with that function.

Any workaround?

lasagna
  • 135
  • 1
  • 10
  • You may use `tryCatch` or `purrr::possibly` – akrun Aug 11 '22 at 17:59
  • How you Call `tryCatch` with this `seedesc= RemDr$findElement(using = "link text", value = "see more")$clickElement()` ? @akrun – lasagna Aug 11 '22 at 18:02
  • Not tested, perhaps you want `tryCatch({RemDr$findElement(using = "link text", value = "see more")$clickElement()}, error = function(e) NA_character_)` – akrun Aug 11 '22 at 18:05
  • keep happening same error @akrun. Code stops after error with selenium – lasagna Aug 11 '22 at 19:08
  • 1
    Here is a similar [case](https://stackoverflow.com/questions/41449082/handle-rselenium-error-messages) which may work for you – akrun Aug 11 '22 at 19:10
  • Can you provide a reproducible example? – Nad Pat Aug 13 '22 at 15:02

1 Answers1

0

Use findElements() which can be accessed in list (list uses double brackets in R i.e. [[1]]) to click found elements:

seedesc <- RemDr$findElements(using = "link text", value = "see more")
seedesc[[1]]$clickElement()

#to click the second found element if there is one
seedesc[[2]]$clickElement() #etc...

When the element is not on the page, seedesc created by findElements() will be a list length 0:

element_exists <- isTRUE(length(seedesc)>0)
Neal Barsch
  • 2,810
  • 2
  • 13
  • 39