Я пытаюсь сохранить результаты цикла for в новых столбцах, но, несмотря на то, что message () возвращает правильную информацию, данные не сохраняются.
Справочная информация:
У меня есть фрейм данных, который в столбце 1 содержит XPaths, а в столбце 2 CSS-селекторы элементов веб-сайта и хочу сохранить результаты getComputedStyle (функция JS, оцененная с помощью RSelenium) для нескольких стилей, таких как «цвет», «ширина» и т. Д.
#Function filling one row with CSS's
get_computed_styles_loop <- function(some_web_element){
for (style in 1:length(css_styles)) {
js_function_styles <- paste("return window.getComputedStyle(",some_web_element,").getPropertyValue('",css_styles[style],"')",sep="")
my_df_col <- columns_number + as.integer(style)
my_df[selector, my_df_col] <- as.character(remDr$executeScript(js_function_styles)) # <- THIS ONE DOESN'T SAVE DATA TO THE DATAFRAME
message(paste(js_function_styles, " ", my_df_col , " ", remDr$executeScript(js_function_styles)))
}
}
#Filling the whole dataframe
for (selector in 1:nrow(my_df){
js_function_xpath <- paste("document.getElementByXPath('",my_df[selector,1],"')",sep="");
js_function_selector <- paste("document.querySelector('",my_df[selector,2],"')",sep="");
tryCatch({
get_computed_styles_loop(js_function_xpath)
},
error=function(cond){
message(paste("XPath not working here: ", selector))
tryCatch({
get_computed_styles_loop(js_function_selector)
},
error=function(cond1){
message(paste("Neither does CSS Selector: ", selector))
return(NA)
}
)
}
)
}
Пример сообщения (). Это выглядит нормально, но не отображается в новых столбцах:
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('color') 22 rgb(17, 17, 17)
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('background-color') 23 rgba(0, 0, 0, 0)
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('background') 24
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('margin') 25
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('padding') 26
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('width') 27 400px
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('height') 28 32px
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('font-weight') 29 400
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('font-family') 30 "Open Sans", sans-serif
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('font-size') 31 13px
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('border') 32
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('border-radius') 33
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('position') 34 static
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('text-align') 35 start
return window.getComputedStyle(document.getElementByXPath('html/body/div[1]/div[1]/table')).getPropertyValue('pointer') 36