'JSON stringified object doesn't add in list [duplicate]
I am trying to add JSON object to an array, but the "bookList" remain empty.
var bookList = [];
for (var i = 0; i < idsArr.length; i++) {
const book = getBook(idsArr[i]).then(book => {return book});
book.then(function(result) {
bookList.push(result);
});
}
result of getBook function:
{"id":1,"ownerId":9,"author":"Aleksandar Lekov","description":"My book part 4","price":15.99,"createdAt":"2022-04-20T09:37:20.252Z","updatedAt":"2022-04-20T09:37:20.252Z"}
getBook function:
async function getBook(id) {
try {
const audiobook = await Audiobook.findOne({
where: {
id: id
},
raw: true,
});
return JSON.stringify(audiobook);
}
catch (err) {
console.log(err, "can't find books by id");
}
}
Solution 1:[1]
The getBook() is an asynchronous call.
bookList = [];
for (var i = 0; i < idsArr.length; i++) {
getBook(idsArr[i]).then(book => {
bookList.push(book);
}).then(console.log); // <-- this should have the books
}
console.log(bookList) // <-- this is empty
So the bookList will be empty when you try to access it right after the for block.
Solution 2:[2]
Try having a look at switch_to_frame, see:
https://selenium-python.readthedocs.io/navigating.html#moving-between-windows-and-frames
iframe = driver.find_element_by_css_selector("iframe")
driver.switch_to_frame(iframe)
elem = driver.find_element_by_css_selector("#user")
Solution 3:[3]
As far I can see, there is no need to switch the frame since this element is out of it.

I couldn't validate my idea, since the HTML code you placed isn't interactable.
I've modified the HTML setting up a default value in this item and it's not giving the value, however, I'm able to get the position and others values from this web element. I would advise to remove the switch method and then double check.
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome('chromedriver.exe') # Optional argument, if not specified will search path.
try:
driver.get('C:\\Users\\user\\IdeaProjects\\chrome-selenium-python\\index.html')
time.sleep(2) # Let the user actually see something!
ele = driver.find_element(By.CSS_SELECTOR, "#user")
ele.send_keys("Example")
time.sleep(2)
print("Location", ele.location)
print("Tag", ele.tag_name)
except Exception as a:
print(a)
finally:
driver.quit()
Solution 4:[4]
wait=WebDriverWait(driver,30)
wait.until(EC.presence_of_element_located((By.ID,"id"))).send_keys('user')
Try for presence.
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | David Fernández Flores |
| Solution 2 | Matt |
| Solution 3 | Nilson Nieto |
| Solution 4 |
