简体   繁体   中英

How do I find these particular elements in a webpage, with selenium using chromedriver?

I have a website that I'm trying to automate but I can't find a particular link with selenium to click on. It looks like a link on the website, but when I use the chrome "inspect" function, it looks like it might be a button (???). I've tried copying the Xpath, but that doesn't work.

Here is the HTML behind the link

<button ng-bind-html="::ListingCtrl.copy.planListing.noPreference" track="No Preference" ng-click="::ListingCtrl.enterNoPreference()" class="link ally-focus-within">No Preference</button>

The Xpath for it is

//*[@id="mainContent"]/div/div/div/div[2]/ul/li[1]/h2/button

The text of the link is "No Preference", so also tried the following

elem_NoPreference = browser.find_element_by_xpath('//@track=\'No Preference\'')

But I'm not sure if my quote escape characters are correct.

I also unsuccessfully tried the following

elem_NoPreference = browser.find_element_by_link_text('No Preference') <br>
elem_NoPreference = browser.find_element_by_class_name('link ally-focus-within') <br>
elem_NoPreference = browser .find_element_by_css_selector("button[class='link ally-focus-within']           

I should mention that the following are unique in the HTML. So, if there is a way to find these using the Xpath, it would be helpful

ListingCtrl.copy.planListing.noPreference 
ListingCtrl.enterNoPreference()
track = "No Preference"

I'm at my wits end here. Any help would be appreciated.

Thank you!

If you don't plan to do any I18N / L10N testing you can stick to No Preference text, in this case the selector would be:

//button[text()='No Preference']

在此处输入图片说明

other option is basically the same, but instead of text it looks for track HTML attribute :

在此处输入图片说明

More information:

Try with this xpath:

elem_NoPreference = browser.find_element_by_xpath("//button[@track='No Preference']")

or, if you want to select the element by the containing text:

elem_NoPreference = browser.find_element_by_xpath("//button[contains(.,'No Preference')]")

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