-
Notifications
You must be signed in to change notification settings - Fork 1.4k
-
Dear Michael,
I have tried some ways to gather a specific element using the "find_element() function". Using this function only give us the power to use the CSS SELECTOR because inside the source code you have hardcoded by= as "CSS_SELECTOR" so I cannot pass inside the BY.XPATH. Later I discovered the method that converts xpath to css selector. The code I have written looks like this :
xpath_for_restoran_sayisi_span = ".//span[contains(text(), 'Number of restaurants')]"
xpath_for_restoran_sayisi_value_span = "./following-sibling::span"
# Convert XPath to CSS selectors
css_selector_for_restoran_sayisi_span = sb.convert_xpath_to_css(xpath_for_restoran_sayisi_span)
css_selector_for_restoran_sayisi_value_span = sb.convert_xpath_to_css(xpath_for_restoran_sayisi_value_span)
# Locate the span with "Number of restaurants" using the CSS selector
restoran_sayisi_span = unique_div.find_element("css selector", css_selector_for_restoran_sayisi_span)
# Locate the sibling span containing the value using the CSS selector
restoran_sayisi_value_span = restoran_sayisi_span.find_element("css selector", css_selector_for_restoran_sayisi_value_span)
restoran_sayisi_value = restoran_sayisi_value_span.text()
However, the code gives an Exception of "cannot convert xpath to css.." How can I use XPATHs with Selenium Base Case methods.
What really I am trying to gather is in this site :
https://www.agoda.com/radisson-blu-hotel-istanbul-ottomare/hotel/istanbul-tr.html?finalPriceView=2&isShowMobileAppPrice=false&cid=1922882&numberOfBedrooms=&familyMode=false&adults=2&children=0&rooms=1&maxRooms=0&checkIn=2024年07月29日&isCalendarCallout=false&childAges=&numberOfGuest=0&missingChildAges=false&travellerType=1&showReviewSubmissionEntry=false¤cyCode=EUR&isFreeOccSearch=false&tag=c4254dc2-e34a-491c-9db8-aeb463b931f1&tspTypes=8&los=1&searchrequestid=4b4d1719-2bc8-411b-bf4b-3ddb699f8516&ds=RHvlky7lTy%2BZYUYU
Here I am trying to gather the number of the "Number of rooms : 133" with Selenium Base function when I am crawling. Please enlighten me about this. And maybe you can help me giving an example code you can construct please..
Screenshot 2024年07月21日 at 00 11 02
Thank you very much for your help!!
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 5 replies
-
Use a better selector, eg:
self.get_text('span div:contains("Number of rooms")')
Output: 'Number of rooms : 133'
Not all XPath selectors can be converted into CSS Selectors. When you can specify the selector by
arg, use by="xpath"
to use xpath. Some methods require CSS Selectors though.
Beta Was this translation helpful? Give feedback.
All reactions
-
Dear Michael, thank you for your quick response.
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 1
-
Why these is no support of XPath selectors, which are more powerful and generic than CSS selectors?
selenium driver supports XPath just fine, I have large tests which use it.
Beta Was this translation helpful? Give feedback.
All reactions
-
XPath is fine to use in regular SeleniumBase modes.
For CDP Mode, that uses the CDP API, which needs CSS Selectors. For XPath selectors that can be converted into CSS Selectors, that's fine too. (Not all XPath can be represented as CSS)
Beta Was this translation helpful? Give feedback.
All reactions
-
I guess that my XPath was not converted to CSS successfully, because in browser console it runs just fine. It's strange why CDP does not support XPath.
Beta Was this translation helpful? Give feedback.
All reactions
-
Can I switch from CDP to regular mode and back to query XPath?
Beta Was this translation helpful? Give feedback.
All reactions
-
sb.reconnect()
/ sb.connect()
to switch back to regular mode.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1