I'm trying to find element by xpath that contains multiple variables and click on it.
I tried using :
oddsnumber = "1.18"
oddstype = "Barcelona"
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#span='"+ oddstype +"' and #span='"+ oddsnumber +"']"))).click()
With only one variable it works, but I need to use multiple in order for script to click on right element.
This is the element it should click on
<div class="gl-Participant gl-Participant_General gl-Market_General-cn3 "><span class="gl-Participant_Name">Barcelona</span><span class="gl-Participant_Odds">1.18</span></div>
<span class="gl-Participant_Name">Barcelona</span>
<span class="gl-Participant_Odds">1.18</span>
Tried to make a script that clicks on element by xpath that matches to multiple variables
You can use this xpath, which targets a div element having two span children with the requested attributes
f"//div[child::span[text()='{oddsnumber}'] and span[text()='{oddstype}']]"
Related
I want to select the Remember Sports part of this line
Remember Sports
I can get the href but how do I get the actual text?
This is what I tried, when I tried to pass another argument into get_attribute or leave it blank then it doesn't work.
browser = webdriver.Chrome('c:\\Users\\16308\\Documents\\VSCPython\chromedriver',chrome_options=chrome_options)
url = 'https://www.allmusic.com/album/sunchokes-mw0003322304'
browser.get(url)
stages = browser.find_element_by_class_name('album-artist')
artist_link = stages.find_element_by_css_selector('a').get_attribute('href')
artist_link_text = stages.find_element_by_css_selector('a')
browser.get(artist_link)
print(artist_link_text)
Thanks for any help.
You can try to use .get_attribute("innerHTML"), if it was an input you could also use 'value' instead of innerHTML.
EDIT:
There is another very similar question: Use Python Selenium to get span text
Select your element using xpath or css then use get text method to get the text inside:
Text=stages.find_element_by_css_selector('a').text
Or
text=driver.findElement(By.className("Class Locator").getText( );
The text Remember Sports is within the following element:
<h2 class="album-artist">
<span>
Remember Sports
</span>
</h2>
Solution
To extract the text you can use either of the following locator strategies:
Using CSS_SELECTOR and get_attribute("innerHTML"):
print(driver.find_element(By.CSS_SELECTOR, "h2.album-artist a[href^='https://www.allmusic.com/artist']").get_attribute("innerHTML"))
Using XPATH and text attribute:
print(driver.find_element(By.XPATH, "//h2[#class='album-artist']//a[#href='https://www.allmusic.com/artist/remember-sports-mn0003731048']").text)
Here is the inspect result for the button that says +5 per day
>span class="text user-links entry-method-title ng-scope ng-binding" ng-include="::'views/entry-text/'+entry_method.entry_type+'.html'">
Click For a Daily Bonus Entry"
</span>
<div class="entry-method bonus template" data-remove-popovers="" id="em6129519" ng-class="{expanded: entryState.expanded == entry_method, template: entry_method.template, 'completed-entry-method': !canEnter(entry_method) && isEntered(entry_method)}" ng-repeat="entry_method in ::entry_methods">
here is the HTML given information when I inspect the link/button, I have tried to use XPath, CSS, link text, and class name and it keeps giving me an error saying it cannot identify the element. Does anyone have a suggestion for how to identify this, it is on gleam.io for a giveaway I'm trying to automate this so i don't have to log in and press this everyday. This is my first ever web interfacing project with python.
Here is my most recent try
driver.maximize_window()
time.sleep(10)
driver.execute_script("window.scrollTo(0, 1440)")
time.sleep(10)
button2 = driver.find_element_by_class_name("text user-links entry-method-title ng-scope ng-binding")
button2.click()
Similar to a previous issue, Selenium find_element_by_class_name and find_element_by_css_selector not working, you can't have spaces in your class name when using driver.find_element_by_class_name. Instead, find the element via css_selector and replace each space with a dot.
driver.find_element_by_css_selector("span.text.user-links.entry-method-title.ng-scope.ng-binding")
That'll fix what you have above, but keep in mind there are other ways to make selenium actions more reliable (eg. WebDriverWait, etc). And there may be a cleaner selector to use than the one above.
I believe the element you want to access is contained within an "iframe", thus you must first switch to iframe before you can access it using selectors.
driver.switch_to.frame(x.find_element_by_xpath("PUT IFRAME XPATH HERE"))
I have a database filled with keywords and I have to get the xpaths of the web elements containing these words. (there is an expandable list button next to every word. to click on that I need to get the xpath of the keyword and modify it to get the button XPath).
I can get the selenium web element using,
keyword_element = driver.find_element_by_xpath(f"//*[contains(text(), '{keyword}')]")
this only gets the selenium web element for the element containing the keyword like this,
<selenium.webdriver.remote.webelement.WebElement (session="1df8dbbae8c93fd772a5134ed0666a7a", element="b3d2bc8d-0c8c-4fb0-b3d1-2a5d49537567")>
and the HTML dev element looks like this.
<span class="fancytree-node actlitetreeitem fancytree-exp-n fancytree-ico-c">
<span class="fancytree-expander"></span>
<span class="fancytree-checkbox"></span>
<span class="fancytree-title">SNAPCHAT</span>
</span>
'SNAPCHAT' is the keyword and I need access to the 'fancytree-checkbox' element. but I have to get it using the keyword SNAPCHAT.
Is there any way to get the xpath of the web element out of this?
Use either of the xpath to identify the element fancytree-checkbox.
Use preceding-sibling
keyword_element = driver.find_element_by_xpath(f"//*[contains(text(), '{keyword}')]/preceding-sibling::span[1]")
OR Identify the parent node and then its child
keyword_element = driver.find_element_by_xpath(f"//span[.//*[contains(text(), '{keyword}')]]//span[#class='fancytree-checkbox']")
I am using Selenium PhantomJS to perform headless dynamic scraping. I was able to extract all information except popups triggered by an ng-click, such as:
<button href="#" ng-click="navigation.login({edu:false})">login</button>
<a class="btn btn-primary" ng-click="login()">signup</a>
I want to get the tag that contains ng-click label, so that I can perform onclick activity and extract information from it.
The ng-click value and tag can be anything, I just want to search whether a tag contains ng-click or not, and if it is then return that tag.
I don't want to use regex or something like that.
The most simple solution is using XPath to check length of value of ng-click.
elements = driver.find_elements_by_xpath("//*[string-length(#ng-click) > 1]")
for element in elements:
element.click()
It works.
elements = driver.find_elements_by_xpath("//*[(#ng-click)]")
my problem is that the first on is clicking and the second one doesnt and i dont undersatand why
WebDriverWait(rootdiv, 10).until(EC.element_to_be_clickable((By.XPATH, "//li[#onclick[contains(.,JTC)]]")))
rootdiv.find_element_by_xpath("//li[#onclick[contains(.,'JTC')]]").click()
WebDriverWait(depdiv, 10).until(EC.element_to_be_clickable((By.XPATH, "//li[#onclick[contains(.,AJU)]]")))
depdiv.find_element_by_xpath("//li[#onclick[contains(.,'AJU')]]").click()
depdiv and root div are children to look under for the certain li cause the root changes from the first to the second..
i've checked that the div is visible and in the first one, its clicking, the second time it cant find the object and im getting a time error
part of the code im trying to fed from..
<div class = "divCombo4">
<ul><li>....</li><ul>
<ul><li>...</li><ul>
</div>
and my depdiv is rooting to the find_element_by_id("divCombo4")
one of those ul li contains
onclick="selecionou('AJU', this,'.txtBusca4', 'false', 'destino', 'Estouem2', 'AJU');"
First of all, you need a dot to make the expressions context-specific. Also, the wait.until() returns a WebElement instance and you can use it instead of issuing a "find element" command again:
WebDriverWait(rootdiv, 10).until(EC.element_to_be_clickable((By.XPATH, ".//li[#onclick[contains(.,JTC)]]"))).click()
WebDriverWait(depdiv, 10).until(EC.element_to_be_clickable((By.XPATH, ".//li[#onclick[contains(.,AJU)]]"))).click()