私信  •  关注

cruisepandey

cruisepandey 最近创建的主题
cruisepandey 最近回复了
4 年前
回复了 cruisepandey 创建的主题 » 使用Python Selenium进行AmCharts抓取

该图形不在Selenium view端口中,因此首先我们必须处理垂直滚动到所需图形的问题,然后我看到了 //*[name()='tspan'] xpath包含水平和垂直值,这些值也存在于UI中。

代码:

driver_path = r'D:\\chromedriver.exe'

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
wait = WebDriverWait(driver, 30)

driver.get("https://eg.pricena.com/en/product/oppo-reno-5g-price-in-egypt")

driver.execute_script("window.scrollTo(0, 1000)")

ele = driver.find_element_by_xpath("//*[name()='svg']")
driver.execute_script("arguments[0].scrollIntoView(true);", ele)

char_val = []
for elem in driver.find_elements(By.XPATH, "//*[name()='tspan']"):
    print(elem.text)

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

输出:

Mar
May
Jul
Sep
Nov
Mar
May
Jul
Sep
Nov
5,000
5,500
6,000
6,500
7,000
7,500
8,000

Process finished with exit code 0
4 年前
回复了 cruisepandey 创建的主题 » 如何编写:如果选择了元素,不要用Python单击Selenium?

您可以像下面这样解析颜色,并将代码包装在try-except子句中

您可以尝试以下代码:

color= "X520"
try:
   if broswer.find_element_by_xpath(f"//*[@class='sku-property-text']//*[text()= '{color}']").is_selected():
      print("already selected")
   else:
       print("It's not selected, so bot is going to click")
       broswer.find_element_by_xpath(f"//*[@class='sku-property-text']//*[text()= '{color}']").click()
       print("Clicked")
except:
    print("Something is wrong.")
    pass
4 年前
回复了 cruisepandey 创建的主题 » 如何在python selenium中获得onclick href=“#”

首选的方式是 LINK_TEXT :

browser.find_element_by_link_text('gaoland.net').click()

或者 WebDriverWait :

 WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "gaoland.net"))).click()

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

如果不起作用,请尝试xpath或css选择器:

  1. xpath:

    //a[contains(@onclick,'showZone') and text()='gaoland.net']
    

//a[contains(@onclick,'showZone')]

css 将是:

a[onclick^='showZone']

你可以用任何一种 driver.find_element_by_xpath('')

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@onclick,'showZone')]"))).click()

附言: 请办理登机手续 dev tools (谷歌浏览器)如果我们有 唯一的 报关进口 HTML DOM 或者不是。

检查步骤:

Press F12 in Chrome ->去 element 第节->做一个 CTRL + F ->然后粘贴 xpath 看看,如果你愿意的话 要素 越来越 突出显示 具有 1/1 匹配节点。

4 年前
回复了 cruisepandey 创建的主题 » 在selenium python中查找特定元素

这是一个svg web元素。您可以(通过xpath)这样定位:

//*[name()='svg' and @aria-label='Add Photo or Video']

点击Selenium基本上有4种方式。

我将使用这个xpath

//*[name()='svg'和@aria label='Add Photo或Video']

代码试验1:

time.sleep(5)
driver.find_element_by_xpath("//*[name()='svg' and @aria-label='Add Photo or Video']").click()

代码试用2:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[name()='svg' and @aria-label='Add Photo or Video']"))).click()

代码试用3:

time.sleep(5)
button = driver.find_element_by_xpath("//*[name()='svg' and @aria-label='Add Photo or Video']")
driver.execute_script("arguments[0].click();", button)

代码试用4:

time.sleep(5)
button = driver.find_element_by_xpath("//*[name()='svg' and @aria-label='Add Photo or Video']")
ActionChains(driver).move_to_element(button).click().perform()

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

附言: 请办理登机手续 dev tools (谷歌浏览器)如果我们有 唯一的 报关进口 HTML DOM 或者不是。

检查步骤:

Press F12 in Chrome ->去 element 第节->做一个 CTRL + F ->然后粘贴 xpath 看看,如果你愿意的话 要素 越来越 突出显示 具有 1/1 匹配节点。

4 年前
回复了 cruisepandey 创建的主题 » Python接受cookies

按钮 Zustimmen iframe 因此,首先你必须切换到相应的iframe,然后你可以与该按钮进行交互。

代码:

driver.maximize_window()
page_url = 'https://www.boerse.de/historische-kurse/Erdgaspreis/XD0002745517'
driver.get(page_url)
wait = WebDriverWait(driver, 30)
try:
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[starts-with(@id,'sp_message_iframe')]")))
    wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Zustimmen']"))).click()
    print('Clicked successfully')
except:
    print('Could not click')
    pass

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
4 年前
回复了 cruisepandey 创建的主题 » Python Selenium通过xpath查找并验证ID

你可以使用 .get_attribute 方法在web元素上执行。您已经将WebElement作为 userSync

比如:

attr_value = userSync.get_attribute('listing-id')
print (attr_value)
4 年前
回复了 cruisepandey 创建的主题 » 如何在python中使用Selenium在Myntra上单击“显示更多”

我没有通读你写的所有代码,但为了点击show more,我尝试了下面的代码,可能你可以用现有代码注入下面的代码。

  1. 我们必须 scroll to that particular element Selenium 知道元素的确切位置。

  2. 我用过JS .click() 点击 展示更多

示例代码:

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
#driver.implicitly_wait(50)
wait = WebDriverWait(driver, 20)
driver.get("https://www.myntra.com/kurtas/jompers/jompers-men-yellow-printed-straight-kurta/11226756/buy")
ele = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.index-showMoreText")))
driver.execute_script("arguments[0].scrollIntoView(true);", ele)
ActionChains(driver).move_to_element(ele).perform()
driver.execute_script("arguments[0].click();", ele)
Complete_The_Look = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "p.index-product-description-content"))).text
print(Complete_The_Look)

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

输出:

Sport this classic kurta from Jompers this season. Achieve a comfortably chic look for your next dinner party or family outing when you team this yellow piece with slim trousers and minimal flair.
4 年前
回复了 cruisepandey 创建的主题 » 如何在python | Selenium中更改网页的HTML代码

你只展示了 options 标签来自 HTML structure .

他们一定是我的孩子 Select tag in HTML .

大体上 Select option 标签用于构建下拉列表。

您可以这样选择它们:

menu = Select(driver.find_element(By.XPATH, "xpath of select web element"))
menu.select_by_value("ua")
4 年前
回复了 cruisepandey 创建的主题 » 无法单击元素,请单击Python

您也可以尝试以下代码:

//img[contains(@src,'registration')]/..

代码:

wait2 = WebDriverWait(driver, 10)
element = wait2.until(EC.element_to_be_clickable((By.XPATH, "//img[contains(@src,'registration')]/..")))
element.click()
4 年前
回复了 cruisepandey 创建的主题 » 如何比较python selenium中的Webelement和int?

请参见代码中的下一行,

if valor.get_attribute('value')>100:
           print("Its over 100")

基本上你是在比较 string 带着 int ,你不能那样做。

相反,你可以试试 int() ,见下文:-

if int(valor.get_attribute('value'))>100:
               print("Its over 100")
4 年前
回复了 cruisepandey 创建的主题 » selenium中使用python的ElementNotInteractiableException

当你使用这个xpath时 //*[@id="search"] 中有5个匹配节点 HTMLDOM .

如果您打算定位如下所示的主搜索栏:

enter image description here

然后可以使用下面的XPath

//input[@id='search']

所以你的有效代码是:

options = uc.ChromeOptions()
options.binary_location = "C:\\Program Files\\BraveSoftware\\Brave-Browser\\Application\\brave.exe"
options.add_argument("--user-data-dir=c:\\temp\\testprofile2")
driver = uc.Chrome(options=options)
driver.maximize_window()


driver.get("https://www.youtube.com/")


def enter_search_term(driver):
    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[@id='search']"))).send_keys("test")
    time.sleep(5)
    driver.quit()
enter_search_term(driver)

我已经测试过了,效果很好。请注意,我也在最大化浏览器。使用 visibility_of_element_located

7 年前
回复了 cruisepandey 创建的主题 » 如何在Selenium python中隐藏锚标记href属性

对于您共享的HTML:

<a id='foo' href='#'></a>  

你可以有一个 Web元素 这样地:

element = driver.find_element_by_id('foo')  

一旦拥有了web元素,就可以获得如下属性:

href_val = element.get_attribute("href")
print(href_val)