Short xPath in R for use with RSelenium

2019-08-13 03:57发布

问题:

I have a problem when using findElement() from RSelenium with xPath on this page where the xPath expression for an element is very long, i.e. the element is nested deeply (I use firefox for the remote driver). findElement() works fine on the page if I use a short xPath expression that I get from inspecting the element e.g. in Google Chrome. However, in R (as far as I know) I can only retrieve the long xPath expression using for example xml_path() from package xml2. Is there a way to get a short xPath expression in R to work around this problem?

library(RSelenium)
url = "http://www.sonova.com/en/media/news"    

remDr = remoteDriver(remoteServerAddr = "localhost", port = 4444, browserName = "firefox")
remDr$open()
remDr$navigate(url)

ShortxPath = "//*[@id='edit_date_filter_value_year_chzn']"
LongxPath = "/html/body/div[2]/div/div/div[1]/div[2]/div/div/div/div[1]/form/div/div/div/div[1]/div/div/div/div/div/div/div/div/div" 

remDr$findElement(using='xpath', ShortxPath)works fine but with remDr$findElement(using='xpath', LongxPath) I get the following error message:

Error: Summary: NoSuchElement
Detail: An element could not be located on the page using the given search parameters.
class: org.openqa.selenium.NoSuchElementException

Any help is much appreciated!

回答1:

No need to use RSelenium. This is the AJAX/XHR request that's made when a popup is selected:

httr::VERB(verb = "POST", url = "http://www.sonova.com/en/views/ajax", 
    add_headers(Origin = "http://www.sonova.com", 
        `Accept-Encoding` = "gzip, deflate", 
        `Accept-Language` = "en-US,en;q=0.8", 
        `User-Agent` = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36", 
        Accept = "application/json, text/javascript, */*; q=0.01", 
        Referer = "http://www.sonova.com/en/media/news", 
        `X-Requested-With` = "XMLHttpRequest", 
        Connection = "keep-alive", 
        DNT = "1"), body = list(`date_filter[value][year]` = "2016", 
        view_name = "news", view_display_id = "page_2", 
        view_args = "", view_path = "media/news", 
        view_base_path = "media/news", 
        view_dom_id = "4f36b0b6d279ba22a46fda6b45ccdd1e", 
        pager_element = "0", `ajax_html_ids[]` = "fit-vids-style", 
        `ajax_html_ids[]` = "style-1-cropbar-clipper", 
        `ajax_html_ids[]` = "logo-title-wrapper", 
        `ajax_html_ids[]` = "logo-title", 
        `ajax_html_ids[]` = "logo-title-logo", 
        `ajax_html_ids[]` = "logo-title-delimiter", 
        `ajax_html_ids[]` = "logo-title-slogan", 
        `ajax_html_ids[]` = "block-superfish-1", 
        `ajax_html_ids[]` = "superfish-1", 
        `ajax_html_ids[]` = "menu-4047-1", 
        `ajax_html_ids[]` = "menu-4046-1", 
        `ajax_html_ids[]` = "menu-3116-1", 
        `ajax_html_ids[]` = "menu-3040-1", 
        `ajax_html_ids[]` = "menu-3048-1", 
        `ajax_html_ids[]` = "menu-7129-1", 
        `ajax_html_ids[]` = "menu-3118-1", 
        `ajax_html_ids[]` = "menu-2960-1", 
        `ajax_html_ids[]` = "menu-3041-1", 
        `ajax_html_ids[]` = "menu-4065-1", 
        `ajax_html_ids[]` = "menu-7067-1", 
        `ajax_html_ids[]` = "menu-3693-1", 
        `ajax_html_ids[]` = "menu-5652-1", 
        `ajax_html_ids[]` = "menu-3045-1", 
        `ajax_html_ids[]` = "menu-3046-1", 
        `ajax_html_ids[]` = "menu-3047-1", 
        `ajax_html_ids[]` = "menu-2716-1", 
        `ajax_html_ids[]` = "menu-4048-1", 
        `ajax_html_ids[]` = "menu-2536-1", 
        `ajax_html_ids[]` = "menu-2796-1", 
        `ajax_html_ids[]` = "menu-2797-1", 
        `ajax_html_ids[]` = "menu-7046-1", 
        `ajax_html_ids[]` = "menu-3966-1", 
        `ajax_html_ids[]` = "menu-3365-1", 
        `ajax_html_ids[]` = "menu-3367-1", 
        `ajax_html_ids[]` = "menu-5651-1", 
        `ajax_html_ids[]` = "menu-7101-1", 
        `ajax_html_ids[]` = "menu-3098-1", 
        `ajax_html_ids[]` = "menu-3122-1", 
        `ajax_html_ids[]` = "menu-5654-1", 
        `ajax_html_ids[]` = "menu-7107-1", 
        `ajax_html_ids[]` = "menu-7106-1", 
        `ajax_html_ids[]` = "menu-3102-1", 
        `ajax_html_ids[]` = "menu-5661-1", 
        `ajax_html_ids[]` = "menu-3106-1", 
        `ajax_html_ids[]` = "menu-3108-1", 
        `ajax_html_ids[]` = "menu-3050-1", 
        `ajax_html_ids[]` = "menu-3051-1", 
        `ajax_html_ids[]` = "menu-3053-1", 
        `ajax_html_ids[]` = "menu-3120-1", 
        `ajax_html_ids[]` = "menu-3054-1", 
        `ajax_html_ids[]` = "menu-7421-1", 
        `ajax_html_ids[]` = "menu-3056-1", 
        `ajax_html_ids[]` = "menu-4067-1", 
        `ajax_html_ids[]` = "menu-4068-1", 
        `ajax_html_ids[]` = "menu-3062-1", 
        `ajax_html_ids[]` = "menu-3064-1", 
        `ajax_html_ids[]` = "menu-3066-1", 
        `ajax_html_ids[]` = "menu-3068-1", 
        `ajax_html_ids[]` = "menu-3072-1", 
        `ajax_html_ids[]` = "menu-3074-1", 
        `ajax_html_ids[]` = "menu-3079-1", 
        `ajax_html_ids[]` = "menu-3076-1", 
        `ajax_html_ids[]` = "menu-3080-1", 
        `ajax_html_ids[]` = "menu-7068-1", 
        `ajax_html_ids[]` = "menu-4708-1", 
        `ajax_html_ids[]` = "menu-4049-1", 
        `ajax_html_ids[]` = "menu-3691-1", 
        `ajax_html_ids[]` = "menu-1890-1", 
        `ajax_html_ids[]` = "menu-3965-1", 
        `ajax_html_ids[]` = "menu-6074-1", 
        `ajax_html_ids[]` = "menu-7045-1", 
        `ajax_html_ids[]` = "menu-7069-1", 
        `ajax_html_ids[]` = "menu-6186-1", 
        `ajax_html_ids[]` = "menu-7098-1", 
        `ajax_html_ids[]` = "menu-3125-1", 
        `ajax_html_ids[]` = "menu-4050-1", 
        `ajax_html_ids[]` = "menu-5632-1", 
        `ajax_html_ids[]` = "menu-7042-1", 
        `ajax_html_ids[]` = "menu-5636-1", 
        `ajax_html_ids[]` = "menu-7410-1", 
        `ajax_html_ids[]` = "menu-5634-1", 
        `ajax_html_ids[]` = "menu-7411-1", 
        `ajax_html_ids[]` = "menu-5639-1", 
        `ajax_html_ids[]` = "menu-5638-1", 
        `ajax_html_ids[]` = "menu-7049-1", 
        `ajax_html_ids[]` = "menu-3284-1", 
        `ajax_html_ids[]` = "menu-7050-1", 
        `ajax_html_ids[]` = "menu-5641-1", 
        `ajax_html_ids[]` = "menu-5646-1", 
        `ajax_html_ids[]` = "menu-7065-1", 
        `ajax_html_ids[]` = "menu-6359-1", 
        `ajax_html_ids[]` = "menu-6372-1", 
        `ajax_html_ids[]` = "menu-6188-1", 
        `ajax_html_ids[]` = "menu-6360-1", 
        `ajax_html_ids[]` = "menu-6362-1", 
        `ajax_html_ids[]` = "menu-6374-1", 
        `ajax_html_ids[]` = "menu-6370-1", 
        `ajax_html_ids[]` = "menu-6376-1", 
        `ajax_html_ids[]` = "menu-6364-1", 
        `ajax_html_ids[]` = "menu-6367-1", 
        `ajax_html_ids[]` = "menu-6368-1", 
        `ajax_html_ids[]` = "menu-7125-1", 
        `ajax_html_ids[]` = "block-apachesolr-panels-search-form", 
        `ajax_html_ids[]` = "apachesolr-panels-search-block", 
        `ajax_html_ids[]` = "edit-apachesolr-panels-search-form", 
        `ajax_html_ids[]` = "edit-actions", 
        `ajax_html_ids[]` = "edit-submit", 
        `ajax_html_ids[]` = "main-content", 
        `ajax_html_ids[]` = "block-block-24", 
        `ajax_html_ids[]` = "views-exposed-form-news-page-2", 
        `ajax_html_ids[]` = "edit-date-filter-wrapper", 
        `ajax_html_ids[]` = "edit-date-filter-value-wrapper", 
        `ajax_html_ids[]` = "edit-date-filter-value", 
        `ajax_html_ids[]` = "edit-date-filter-value", 
        `ajax_html_ids[]` = "edit-date-filter-value-year", 
        `ajax_html_ids[]` = "edit_date_filter_value_year_chzn", 
        `ajax_html_ids[]` = "edit_date_filter_value_year_chzn_o_0", 
        `ajax_html_ids[]` = "edit_date_filter_value_year_chzn_o_1", 
        `ajax_html_ids[]` = "edit_date_filter_value_year_chzn_o_2", 
        `ajax_html_ids[]` = "edit_date_filter_value_year_chzn_o_3", 
        `ajax_html_ids[]` = "edit_date_filter_value_year_chzn_o_4", 
        `ajax_html_ids[]` = "edit_date_filter_value_year_chzn_o_5", 
        `ajax_html_ids[]` = "edit_date_filter_value_year_chzn_o_6", 
        `ajax_html_ids[]` = "edit_date_filter_value_year_chzn_o_7", 
        `ajax_html_ids[]` = "edit_date_filter_value_year_chzn_o_8", 
        `ajax_html_ids[]` = "edit_date_filter_value_year_chzn_o_9", 
        `ajax_html_ids[]` = "edit_date_filter_value_year_chzn_o_10", 
        `ajax_html_ids[]` = "edit_date_filter_value_year_chzn_o_11", 
        `ajax_html_ids[]` = "edit_date_filter_value_year_chzn_o_12", 
        `ajax_html_ids[]` = "edit_date_filter_value_year_chzn_o_13", 
        `ajax_html_ids[]` = "edit_date_filter_value_year_chzn_o_14", 
        `ajax_html_ids[]` = "edit-submit-news", 
        `ajax_html_ids[]` = "block-menu-block-1", 
        `ajax_html_ids[]` = "block-panels-mini-social-media-sidebar-media", 
        `ajax_html_ids[]` = "mini-panel-social_media_sidebar_media", 
        `ajax_html_ids[]` = "block-panels-mini-footer-teaser-media", 
        `ajax_html_ids[]` = "mini-panel-footer_teaser_media", 
        `ajax_html_ids[]` = "block-block-14", 
        `ajax_html_ids[]` = "block-locale-language", 
        `ajax_html_ids[]` = "block-menu-block-2", 
        `ajax_html_ids[]` = "block-block-1", 
        `ajax_html_ids[]` = "block-block-30", 
        `ajax_page_state[theme]` = "sonova", 
        `ajax_page_state[theme_token]` = "iUh534BYiVjPdWDZAWTaoKWaP7PSCw9itl7qz8ZHnk0", 
        `ajax_page_state[css][0]` = "1", 
        `ajax_page_state[css][1]` = "1", 
        `ajax_page_state[css][2]` = "1", 
        `ajax_page_state[css][3]` = "1", 
        `ajax_page_state[css][4]` = "1", 
        `ajax_page_state[css][5]` = "1", 
        `ajax_page_state[css][sites/all/themes/omega/css/modules/system/system.base.css]` = "1", 
        `ajax_page_state[css][sites/all/themes/omega/css/modules/system/system.menus.theme.css]` = "1", 
        `ajax_page_state[css][sites/all/themes/omega/css/modules/system/system.messages.theme.css]` = "1", 
        `ajax_page_state[css][sites/all/themes/omega/css/modules/system/system.theme.css]` = "1", 
        `ajax_page_state[css][sites/all/modules/date/date_api/date.css]` = "1", 
        `ajax_page_state[css][sites/all/modules/date/date_popup/themes/datepicker.1.7.css]` = "1", 
        `ajax_page_state[css][sites/all/themes/omega/css/modules/field/field.theme.css]` = "1", 
        `ajax_page_state[css][sites/all/modules/mollom/mollom.css]` = "1", 
        `ajax_page_state[css][modules/node/node.css]` = "1", 
        `ajax_page_state[css][sites/all/themes/omega/css/modules/search/search.theme.css]` = "1", 
        `ajax_page_state[css][sites/all/modules/custom/social_media/css/social-media-icons.css]` = "1", 
        `ajax_page_state[css][sites/all/themes/omega/css/modules/user/user.theme.css]` = "1", 
        `ajax_page_state[css][sites/all/themes/omega/css/modules/user/user.base.css]` = "1", 
        `ajax_page_state[css][sites/all/modules/views/css/views.css]` = "1", 
        `ajax_page_state[css][sites/all/libraries/chosen/chosen/chosen.css]` = "1", 
        `ajax_page_state[css][sites/all/modules/chosen/css/chosen-drupal.css]` = "1", 
        `ajax_page_state[css][sites/all/modules/ckeditor/ckeditor.css]` = "1", 
        `ajax_page_state[css][sites/all/modules/ctools/css/ctools.css]` = "1", 
        `ajax_page_state[css][sites/all/modules/custom/custom/js/iealert/style.css]` = "1", 
        `ajax_page_state[css][sites/all/modules/panels/css/panels.css]` = "1", 
        `ajax_page_state[css][sites/all/modules/views_slideshow/views_slideshow.css]` = "1", 
        `ajax_page_state[css][public://ctools/css/4c9817ec89d2eea25e2d03fdbe3caf01.css]` = "1", 
        `ajax_page_state[css][public://ctools/css/3e409a6b8a8403f7c6fad84e85cb2c4c_2.css]` = "1", 
        `ajax_page_state[css][public://ctools/css/7e4a5ede216a8e29a390a3d19204cb02_5.css]` = "1", 
        `ajax_page_state[css][public://ctools/css/b28b41cf976f7b854611d66497c6663a_2.css]` = "1", 
        `ajax_page_state[css][public://ctools/css/43131ccf35fa0c1e323794d8af618676_1.css]` = "1", 
        `ajax_page_state[css][sites/all/modules/panels/plugins/layouts/flexible/flexible.css]` = "1", 
        `ajax_page_state[css][public://ctools/css/c4399fe142d4864f78b0cc8cd615f450_1.css]` = "1", 
        `ajax_page_state[css][sites/all/modules/flexslider/assets/css/flexslider_img.css]` = "1", 
        `ajax_page_state[css][sites/all/libraries/flexslider/flexslider.css]` = "1", 
        `ajax_page_state[css][sites/all/libraries/superfish/css/superfish.css]` = "1", 
        `ajax_page_state[css][sites/all/libraries/superfish/css/superfish-smallscreen.css]` = "1", 
        `ajax_page_state[css][sites/all/themes/sonova/css/reset.css]` = "1", 
        `ajax_page_state[css][sites/all/themes/sonova/css/styles.css]` = "1", 
        `ajax_page_state[css][sites/all/themes/sonova/css/print.css]` = "1", 
        `ajax_page_state[js][0]` = "1", 
        `ajax_page_state[js][1]` = "1", 
        `ajax_page_state[js][sites/all/modules/chosen/chosen.js]` = "1", 
        `ajax_page_state[js][sites/all/modules/jquery_update/replace/jquery/1.7/jquery.min.js]` = "1", 
        `ajax_page_state[js][misc/jquery.once.js]` = "1", 
        `ajax_page_state[js][misc/drupal.js]` = "1", 
        `ajax_page_state[js][sites/all/modules/jquery_update/replace/ui/external/jquery.cookie.js]` = "1", 
        `ajax_page_state[js][sites/all/modules/jquery_update/replace/misc/jquery.form.min.js]` = "1", 
        `ajax_page_state[js][misc/ajax.js]` = "1", 
        `ajax_page_state[js][sites/all/modules/jquery_update/js/jquery_update.js]` = "1", 
        `ajax_page_state[js][sites/all/modules/custom/custom/js/common.js]` = "1", 
        `ajax_page_state[js][sites/all/themes/omega/libraries/html5shiv/html5shiv.min.js]` = "1", 
        `ajax_page_state[js][sites/all/libraries/chosen/chosen/chosen.jquery.min.js]` = "1", 
        `ajax_page_state[js][sites/all/modules/custom/custom/js/iealert.min.js]` = "1", 
        `ajax_page_state[js][sites/all/modules/iframe/iframe.js]` = "1", 
        `ajax_page_state[js][sites/all/modules/panels/js/panels.js]` = "1", 
        `ajax_page_state[js][sites/all/modules/views_slideshow/js/views_slideshow.js]` = "1", 
        `ajax_page_state[js][sites/all/modules/ctools/js/auto-submit.js]` = "1", 
        `ajax_page_state[js][sites/all/modules/views/js/base.js]` = "1", 
        `ajax_page_state[js][misc/progress.js]` = "1", 
        `ajax_page_state[js][sites/all/modules/views/js/ajax_view.js]` = "1", 
        `ajax_page_state[js][sites/all/modules/google_analytics/googleanalytics.js]` = "1", 
        `ajax_page_state[js][sites/all/libraries/flexslider/jquery.flexslider-min.js]` = "1", 
        `ajax_page_state[js][sites/all/themes/sonova/js/jquery.iframeResizer.min.js]` = "1", 
        `ajax_page_state[js][sites/all/libraries/superfish/sfsmallscreen.js]` = "1", 
        `ajax_page_state[js][sites/all/libraries/superfish/superfish.js]` = "1", 
        `ajax_page_state[js][sites/all/modules/superfish/superfish.js]` = "1", 
        `ajax_page_state[js][sites/all/themes/sonova/js/jquery.stacktable.js]` = "1", 
        `ajax_page_state[js][sites/all/themes/sonova/js/jquery.browser.min.js]` = "1", 
        `ajax_page_state[js][sites/all/themes/sonova/js/general.js]` = "1", 
        `ajax_page_state[js][sites/all/themes/sonova/js/jquery.fitvids.js]` = "1", 
        `ajax_page_state[js][sites/all/themes/sonova/js/jquery.backgroundSize.js]` = "1", 
        `ajax_page_state[js][sites/all/themes/sonova/libraries/selectivizr/selectivizr.js]` = "1", 
        `ajax_page_state[js][sites/all/themes/sonova/libraries/respond/respond.min.js]` = "1", 
        `ajax_page_state[jquery_version]` = "1.7"), 
    encode = "form")

Figure out the params & now you can use R w/o browser instrumentation or XPath hacking.

This was created via curlconverter



标签: r rselenium xml2