Hey everyone! Today I am working with Python 3 and Selenium web driver. I am encountering a problem with finding by element. Here is the HTML:
<a _ngcontent-pxc-c302="" class="name ng-star-inserted" href="/person/20d4a795d3fb43bdbee7e480df27b05b">michele regina</a>
The goal is to click on the first name that appears in a listed column.
The name changes with every page refresh. This comes with two problems in that the link text is the name and it changes, and the link constantly changes as well for each different name except for the part that says /person/
I tried the following:
driver.find_element_by_css_selector("a.name ng-star-inserted").click()
driver.find_element_by_class_name("name ng-star-inserted").click()
Which resulted in an element that is not a clickable error.
I also tried xpath by copying and pasting the entire XPath copied from Google inspector... lol
driver.find_element_by_xpath('/html/body/app-root/app-container/div/div/tc-app-view/div/div/div/ng-component/mat-sidenav-container/mat-sidenav-content/div/div[2]/tc-person-table-container/tc-collapse-expand[1]/section/tc-person-table/table/tbody/tr[1]/td[2]/tc-person-table-cell/a').click()
and it kind of works sometimes, which is weird but I am sure there has to be a better way. Thank you so much in advance! Any help is appreciated! Also, I am very new to Python, I know zero HTML, and I am also newer to stack! Thanks!
From the given full xpath the a tag you are looking for is in table,section,div. Look for an id in those tags and trace the a.
Related
I am currently writing a script for browser automation using selenium and I need to click a button on a webpage. I have successfully found and identified a bunch of buttons to press using driver.find_element(By.ID,'name').click() but when I get to one of the buttons I need to press there is no ID and this is the full element: <a tabindex="-1" href="/miscOperations/theWall?dash=true">The Wall</a>
I am running into issues using the By.XPATH function of find element because of the '?' within the href. Is there anyway to search for the string "The Wall" and find the element? I think this would be my solve since it is a unique ID within the HTML but I am not sure how to find a certain string within the HTML. Another way I could possibly do it is to find a way to put the '?' character into Python without causing a syntax error due to the character itself even though its within a string.
Do you guys have an idea on the best way to go about this?
Thanks so much!
driver.find_element(By.LINK_TEXT,"The Wall").click()
It's in an a tag so you can use Link text.
I have got this piece of html code:
<span class="amount ng-binding ng-scope" ng-if="object.numberOfReactions !== null">41</span>
I want to acces the 41 number by using driver.find_elements_by_xpath,
my best try is
driver.find_elements_by_xpath(//div[#ng.if='object.numberOfReactions !== null'])but this gives an empty list []. Help would be greatly appreciated.
I know that there are multiple ways to write the xpath, but if you go to the html code, highlight it, and right click you should see options that allow you to copy the full xpath. I personally use this alot because it's really fast and easy to use
Xpath is the property to element in browser. It doesn't mater which language we are using. XML path will be the same. Use to find xpath technique.
In python we can find element using driver.find_elements_by_xpath
Iv been trying to get the form elements on this website and for some reason I cant get them. Iv tried every way I can think of. I would really appreciate some help.
Here is my latest attempt
inputs = driver.find_elements_by_xpath("//input[#type='text']")
Here is the website.
Thanks in advance.
Edit : Github link for project
Edit : Ok so I found out that if you go straigt to website that the btn points to then it works. So I am assuming that slenium does not automaticly update to the HTML of the current page. How does one do this?
if you are getting 'NoSuchElementException', try using conditional wait before you do find_element. When you are loading another page wait until the element is visible and can be interacted.
Guide for that is here
check if the element is inside in iform, if so use
driver.switchTo().frame("framename")
EDIT
Focus on something unique in the tag. For your example use the class name used in the div. Do not use the ID tag identifier as it changes over time.
//*[contains(#class, 'ginput_container ginput_container_text')]/following::*[contains(text(),'Email')]/following::div[1]/descendant::input
I don't usually post on here as you all seem pretty advanced for me, but this feels like an advanced question, so thought I'd ask.
I'm using the Content Egg plugin for WordPress (please don't roll eyes) and it's asking me for XPath for a price I'm trying to get. I have tried so many different xpaths and none of them seem to be working. I'm trying to get the price Xpath from this webpage. Any ideas?
https://www.hockeydirect.com/Catalogue/Hockey-Sticks/Young-Ones-Hockey-Sticks/Young-Ones-ABD-Hockey-Sticks/Young-Ones-ABD-70-Hockey-Stick-341111
This is what i've tried:
//html/body/form/div[5]/div[2]/div[3]/div/span/div[1]/div[4]/div[2]/div[2]/div/div[1]/div/span/fieldset/div[1]/div/div[1]
//*div[#class="MainPriceContainer"]
//div[#class='MainPriceContainer vcenter']
Any help would be greatly appreciated.
try to copy the xpath from firefox by this steps
inspect elements and select the price with the selector pointer it will highlight the element in the inspector html
then right click to the highlighted code then copy then select XPATH from inside copy
then paste it to the Xpath
then set the price update in the offer module setting to 30 seconds and test
if worked tell me :)
It is a href I am trying to click on this using the below code however, it is not able to find link text. It has no frames and it is on the same window. Not sure what is going on
self.driver.find_element_by_link_text("UNITED WAY OF EASTERN UTAH").click()
This is the screenshot of the element code:
Wild guess here, but has the page fully loaded (including any content created by dynamic code e.g. javascript) before you try to click on the link. If the link is created after you try to find it then obviously it will be missing. Try putting a time.sleep before you try to find it.
Took a closer look to the screenshot and this should help. You are using find_element_by_link_text which if I am not mistaken looks for a complete match between the provided text and the text in the link. However the text in your link is not an exact match. You should use find_element_by_partial_link_text instead