简体   繁体   中英

TypeError: Object of type RelativeBy is not JSON serializable error using relative_locator through Selenium Python

I'm trying to write my own flight tracker using selenium and chrome driver. In order to get to the input box, I'm trying to travel down the tree of embedded html elements (for some reason I can't get straight to it, even using the xpath attribute in webdriver).

I tried the solution from this thread but it did not work (currently using a newer version of selenium, so this solution might be outdated)

Here is my code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with

driver_service = Service(executable_path = '/path/chromedriver')
url = 'https://www.google.com/travel/flights'
browser = webdriver.Chrome(service = driver_service)
browser.get(url)
bigBody = browser.find_element(By.ID,'yDmH0d')
#WebElement data type
wrongWiz = bigBody.find_element(By.ID, 'ow4')
wizLoc = locate_with(By.TAG_NAME, 'c-wiz').below(wrongWiz)
bigWiz = bigBody.find_elements(wizLoc) 
##This part is what throws the error, whether I'm using element or elements. 

Here's the error:

TypeError: Object of type RelativeBy is not JSON serializable

This part of the code:

locate_with(By.TAG_NAME, 'c-wiz')

though a legitimate Relative Locators based expression, but still you have to pass it as an argument to browser.find_element() as follows:

wizLoc = driver.find_element(locate_with(By.TAG_NAME, 'c-wiz').below(wrongWiz))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM