I have the bellow code and i want to extract the text in the parenthesis i.e '/team/barcelona/SKbpVP5K/'
<span class="team_name_span">
<a onclick="javascript:getUrlByWinType('/team/barcelona/SKbpVP5K/');">Barcelona</a></span>
Any way to do that?
best regards,
Nick
Below will print javascript:getUrlByWinType('/team/barcelona/SKbpVP5K/');. Assuming you define your driver and go the the url first. In my opinion trimming stings is a separate question but if needed I can add to this answer to help with that.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
(By.XPATH, "//span[#class='team_name_span']//a")))
text_i_want = element.get_attribute("onclick")
print(text_i_want)
Related
I would like to click on one of these tabs but I am not able to access it.
How can I access the data-toggle with Selenium in python?
Please try something like this:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#class=='section-tabs']//a[contains(#href,'timeseries-tab')]"))).click()
The code above should be able to select the timeseries tab.
You did not share the xml of the other tab, but I guess this should work:
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#class=='section-tabs']//a[contains(#href,'heatplot-tab')]"))).click()
This can be also done with CSS Selectors.
To give better answer we need the link to that page or at least the entire XML of that page as a text, not picture
I was hoping for maybe some clarification or suggestions on how to get selenium working with hyperlink. I have tried selecting it by pretty much every element possible. I have also attached a image with the source to look at and compare...
Example of html:
<div class="x-grid3-cell-inner x-grid3-col-ACTION_COLUMN" id="5005a00001rJ22q_ACTION_COLUMN"><span>Edit</span> | </div>
Examples:
#clicky = driver.find_element_by_xpath('//a[contains(#href,"javascript:srcUp(%27%2F5005a00001pKfzm%3Fisdtp%3Dvw%27);")]').click()
#clicky = driver.find_elements_by_partial_link_text('javascript:srcUp(%27%2F5005a00001pKfzm%3Fisdtp%3Dvw%27);').click()
#clicky = driver.find_element_by_xpath("//a[#href='javascript:srcUp(%27%2F5005a00001pKfzm%3Fisdtp%3Dvw%27);']").click()
Try this:
driver.find_element_by_xpath("//div[.//a[contains(#class,'chatterFollowUnfollowAction')]]//a[contains(#href,'javascript:srcUp')]").click()
In order to add an explicit wait before accessing this element use this:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[.//a[contains(#class,'chatterFollowUnfollowAction')]]//a[contains(#href,'javascript:srcUp')]"))).click()
Try this:
someElement = driver.find_element_by_xpath("//a[contains(#class, 'chatterFollowUnfollowAction' and #title,'Follow this case')]")
driver.execute_script("arguments[0].click();",someElement)
I am trying to determine the number of pages of data generated by the Indian Central Pollution Controal Board. Here is an example of output.
Following https://github.com/RachitKamdar/Python-Scraper, I used selenium/python
maxpage = int(browser.find_elements(By.XPATH,"//*[#id='DataTables_Table_0_paginate']/span/a")[-1].text)
but this produces an empty array. I am really not sure what I am doing wrong. Any help would be greatly appreciated. Thanks
You have to add expected condition to wait until the page loaded the data.
You can wait for visibility of element you are using and after that get it's text, like this:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//*[#id='DataTables_Table_0_paginate']/span/a")))
maxpage = int(browser.find_elements(By.XPATH,"//*[#id='DataTables_Table_0_paginate']/span/a")[-1].text)
You might want to try getattribute('textContent')
In your case:
maxpage=browser.find_element_by_xpath("(//*[#id='DataTables_Table_0_paginate']/span/a)[last()]").getattribute('textContent')
Basically, I want to navigate from this website https://www.neds.com.au/racing to this page https://www.neds.com.au/racing/belmont/2d6e712e-298b-4061-8e4d-fbef8c754b85.
There is not one xpath I can use though. The xpath's are:
//*[#id="page-content"]/div/div[1]/div[2]/div[2]/div/ul/li[5]/a/div[1]/span (Belmont)
//*[#id="page-content"]/div/div[1]/div[2]/div[2]/div/ul/li[5]/ul/li[9]/a/span[1] (R9)
This should do it. Note there is a space after 'Belmont '.
driver.find_element_by_xpath("//span[.='Belmont ']").click()
A more robust way would be using WebDriverWait:
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH,"//span[.='Belmont ']"))).click()
, which requires these imports:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
I am trying to execute this code and it shows 'NoSuchElementException' this error.
So can anyone help me for this ?
Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver= webdriver.Chrome()
page=driver.get("http://www.awgp.org")
banner_tell=driver.find_element_by_Link_text('Tell Me More')
banner_tell.click()
I think all you need to do is give link text in uppercase but this link is dynamic as the banner auto-slides to the next one.
You should come up with another locator to click on exactly the locator you want to click. Otherwise you might get ElementNotVisibleException if the banner is changed.
banner_tell=driver.find_element_by_link_text('TELL ME MORE')
try with xpath
banner_tell= driver.find_element_by_xpath("//*[contains(text(),'TELL ME MORE')]")
Seems you were almost near however the function() should have been :
find_element_by_link_text()
To click on the button with text as TELL ME MORE you have to induce WebDriverWait with expected_conditions clause element_to_be_clickable as follows :
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "TELL ME MORE"))).click()