Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Base Case Selenium sb.convert_xpath_to_css() with xpath not working #2943

Orbiszeus started this conversation in General
Discussion options

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&currencyCode=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!!

You must be logged in to vote

Replies: 2 comments 5 replies

Comment options

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.

You must be logged in to vote
1 reply
Comment options

Dear Michael, thank you for your quick response.

Comment options

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.

You must be logged in to vote
4 replies
Comment options

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)

Comment options

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.

Comment options

Can I switch from CDP to regular mode and back to query XPath?

Comment options

sb.reconnect() / sb.connect() to switch back to regular mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /